Use OpenCSV framework to implement CSV data import and export function tutorial in the Java library
Use OpenCSV framework to implement CSV data import and export function tutorial in the Java library
In Java applications, processing CSV (comma segmental value) file is a common task.The CSV file is separated by the comma from each data field.OpenCSV is a popular Java class library that can be used to read and write CSV files.This tutorial will show you how to use the OpenCSV framework to implement the CSV data import and export function in the Java class library.
1. Install OpenCSV
First of all, you need to use the OpenCSV framework, you need to add it to the dependency item of the Java project.You can add the following code to the pom.xml file of the project to add OpenCSV Maven dependency items:
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.5.1</version>
</dependency>
If you do not use Maven for project management, you can also download the jar file from OpenCSV's official website (https://opencsv.sourceForge.io/) and add it to the project's path.
2. Import CSV file
To introduce CSV files, you need to create a CSVReader object and use its readall () method to read all rows in the CSV file.The following is a sample code that shows how to import a CSV file named "Data.csv" and print out its content:
import com.opencsv.CSVReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
public class CSVImportExample {
public static void main(String[] args) {
String csvFile = "data.csv";
try (CSVReader reader = new CSVReader(new FileReader(csvFile))) {
List<String[]> rows = reader.readAll();
for (String[] row : rows) {
for (String cell : row) {
System.out.print(cell + "\t");
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the above code, the path of the CSV file is specified as "data.csv".Use the Try-With-Resources statement to open the CSVReader object, and use the readall () method to read the contents of the CSV file into a list.Then, by traversing the lines and columns in the list, you can access each cell in the CSV file and perform the corresponding operation.
3. Export CSV file
To export the CSV file, you need to create a CSVWriter object, and use its WriteAll () method to write all rows into the CSV file.The following is an example code that shows how to export a CSV file containing names and age:
import com.opencsv.CSVWriter;
import java.io.FileWriter;
import java.io.IOException;
public class CSVExportExample {
public static void main(String[] args) {
String csvFile = "data.csv";
try (CSVWriter writer = new CSVWriter(new FileWriter(csvFile))) {
String[] header = {"Name", "Age"};
writer.writeNext(header);
String[] row1 = {"Alice", "25"};
writer.writeNext(row1);
String[] row2 = {"Bob", "30"};
writer.writeNext(row2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the above code, use Try-With-Resources statement to create a CSVWriter object, and use the Writnext () method to write the title line and data line of the CSV file.Each line is a string array, which contains the value of each cell in the CSV file.
Summarize:
This tutorial shows you how to use the OpenCSV framework to implement the CSV data import and export function in the Java class library.By importing CSV files, you can read and process data.By exporting the CSV file, you can write the data into the new CSV file.OpenCSV provides simple and powerful tools and methods, making processing CSV files easier.
I hope this tutorial will help you understand and use the OpenCSV framework.