How to use the OpenCSV framework in the Java class library to process the CSV file
How to use the OpenCSV framework in the Java class library to process the CSV file
Import the OpenCSV library
To use the OpenCSV framework to process the CSV file in the Java project, we need to import the library into the project.You can add OpenCSV dependencies to the project's construction file (such as Pom.xml), or manually add OpenCSV's jar file to the project's class path.
Reading CSV file
Before starting the CSV file, you need to determine the path of the CSV file.You can use Java's file class or other file processing classes to obtain the path of the CSV file.Once we have the path of the file, we can use the CSVReader class in the OpenCSV library to read the data in the CSV file.
Below is an example code that reads CSV files using OpenCSV:
import java.io.FileReader;
import java.io.IOException;
import com.opencsv.CSVReader;
public class CSVReaderExample {
public static void main(String[] args) {
try {
// Create a CSVReader object and pass the path of CSV files
CSVReader reader = new CSVReader(new FileReader("path/to/csv/file.csv"));
String[] line;
// Read the data in the CSV file row
while ((line = reader.readNext()) != null) {
// Process data in CSV files
for (String data : line) {
System.out.print(data + " ");
}
System.out.println();
}
// Close the CSVReader object
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the above example code, we first created a CSVReader object and passed the path of the CSV file to it.Then read the data in the CSV file with the WHILE cycle.For each line, we use a for loop to traverse each data in the line and print it out.Finally, we closed the CSVReader object to release related resources.
Write into CSV file
If you want to write the data into the CSV file, you can use the CSVWRiter class in the OpenCSV library.The following is an example code that uses OpenCSV to write data to the CSV file:
import java.io.FileWriter;
import java.io.IOException;
import com.opencsv.CSVWriter;
public class CSVWriterExample {
public static void main(String[] args) {
try {
// Create CSVWRiter objects and specify the path of CSV file
CSVWriter writer = new CSVWriter(new FileWriter("path/to/csv/file.csv"));
// Write the data into the CSV file
String[] data1 = {"John", "Doe", "john.doe@example.com"};
String[] data2 = {"Jane", "Smith", "jane.smith@example.com"};
writer.writeNext(data1);
writer.writeNext(data2);
// Close the CSVWRiter object
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the above sample code, we first created a CSVWriter object and passed the path of the CSV file for it.Then, we define the data to be written to the CSV file, and use the writer.writenext () method to write the data to the file.Finally, we closed the CSVWriter object to refresh the data into the CSV file on the disk.
Summarize
Through the OpenCSV framework, we can easily read and write CSV files.When reading the CSV file, we can read data with the CSVReader class.For writing CSV files, we can use the CSVWriter class to write the data into the file.It is hoped that this article will help using OpenCSV to process CSV files. If there are more problems, please refer to the official documentation of OpenCSV or seek more resources for learning.