Use OpenCSV to implement CSV files read and write
Use OpenCSV to implement CSV files read and write
CSV (COMMA-SEPARATED VALUES) is a commonly used file format that can be used for storage and exchange form data.OpenCSV is an open source Java library that can easily read and write CSV files.
CSV file read:
To read the CSV file with OpenCSV, you first need to import the OpenCSV library.You can introduce the OpenCSV library by Maven or manually downloading jar files.
The steps to read the CSV file are as follows:
1. Create a CSVReader object and specify the CSV file path to be read.
2. Use the method of the CSVReader object to read every line of data in the CSV file in order and return a String array.
3. Traversing each line of data, you can access each column of data through the index of array.
The following is an example code that demonstrates how to use OpenCSV to read CSV files and print the data of each line:
import com.opencsv.CSVReader;
import java.io.FileReader;
import java.io.IOException;
public class CSVReaderExample {
public static void main(String[] args) {
String csvFile = "data.csv";
try (CSVReader reader = new CSVReader(new FileReader(csvFile))) {
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
for (String data : nextLine) {
System.out.print(data + " ");
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the above code, `data.csv` indicates the CSV file path to be read.By creating the CSVReader object and using the method of `Reader.readnext ()`, we can read the data of the CSV files one by one and traverse the columns of each line through the `For` cycle.
CSV file writing:
It is relatively simple to write the CSV file with OpenCSV.
The steps written to the CSV file are as follows:
1. Create a CSVWriter object and specify the CSV file path to be written.
2. Call the method of the CSVWRiter object's `writenext () method, and pass in a String array, indicating that each line of data to be written is written.
3. You can use a cycle to write multi -line data.
4. Finally call the `close () method of the CSVWriter object to close the file.
The following is a sample code that demonstrates how to use OpenCSV to write the data to the CSV file:
import com.opencsv.CSVWriter;
import java.io.FileWriter;
import java.io.IOException;
public class CSVWriterExample {
public static void main(String[] args) {
String csvFile = "data.csv";
try (CSVWriter writer = new CSVWriter(new FileWriter(csvFile))) {
String[] header = {"Name", "Age", "City"};
writer.writeNext(header);
String[] data1 = {"John", "25", "New York"};
String[] data2 = {"Jane", "30", "San Francisco"};
writer.writeNext(data1);
writer.writeNext(data2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the above code, `data.csv` indicates the CSV file path to be written.By creating a CSVWriter object and using the method of `writer.writenext (), we can write data one by one.The head data is written in the example, and then two lines of data are written through the method of `` `` `` `` `` `writer.writenext () `.
In summary, using OpenCSV can easily read and write CSV files.Through OpenCSV's CSVReader and CSVWriter class, we can flexibly process CSV data to achieve more advanced functions, such as filtering data and adding new lines.