Commons CSV (SANDBOX) framework in the JAVA Library Overview
The Commons CSV (sandbox) framework is a Java class library for processing the comma -separated value (CSV) file.It provides a simple and flexible way to read and write into the CSV file.This article will outline the technical principles of the Commons CSV framework and provide some Java code examples.
CSV file is a common data exchange format that is usually used to store table data in text files.Each row represents a record, and each field is separated with a comma.The Commons CSV framework provides a convenient way to read and write this file.
Before using the Commons CSV framework, the corresponding dependencies need to be introduced.This can be implemented by adding the following Maven dependencies to the construction file of the project:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.8</version>
</dependency>
After introducing dependencies, we can start using the Commons CSV framework.
Below is a sample code that reads and analyze the CSV file with the Commons CSV framework:
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class CSVReaderExample {
public static void main(String[] args) throws IOException {
Reader reader = new FileReader("data.csv");
CSVParser csvParser = CSVFormat.DEFAULT.parse(reader);
for (CSVRecord csvRecord : csvParser) {
String name = csvRecord.get(0);
int age = Integer.parseInt(csvRecord.get(1));
String email = csvRecord.get(2);
System.out.println("Name : " + name);
System.out.println("Age : " + age);
System.out.println("Email : " + email);
System.out.println("==========================");
}
csvParser.close();
reader.close();
}
}
In the above example, we first create a Filereader object to read the CSV file.Then use CSVFormat.Default to create a CSVPARSER object.By traversing each CSVRecord object of CSVPARSER, we can easily obtain the field value of each line.The example shows how to obtain the name, age and email field of each row and print it out.
Similarly, the Commons CSV framework also provides some methods to write the CSV file.The following is a code for writing data to CSV files:
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.List;
public class CSVWriterExample {
public static void main(String[] args) throws IOException {
Writer writer = new FileWriter("output.csv");
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT);
List<String> record1 = Arrays.asList("John", "30", "john@example.com");
List<String> record2 = Arrays.asList("Jane", "25", "jane@example.com");
List<String> record3 = Arrays.asList("Bob", "35", "bob@example.com");
csvPrinter.printRecord(record1);
csvPrinter.printRecord(record2);
csvPrinter.printRecord(record3);
csvPrinter.close();
writer.close();
}
}
In the above example, we first create a FileWriter object to write to the CSV file.Then use CSVFormat.Default to create a CSVPrinter object.By calling the PrintRecord () method of CSVPrinter, we can write the field value of each line into the CSV file.
These examples show some common usage of the Commons CSV framework.By using this framework, we can easily read and write the CSV file without manual processing separation symbols and quotes.The Commons CSV framework provides more advanced features, such as processing different separators, custom formats and configurations, making the processing of CSV files more flexible and powerful.