Learn from the core function and API of the Apache Commons CSV framework
Apache Commons CSV is an open source Java framework for reading and writing to CSV files.It provides a set of simple and easy -to -use APIs for processing, analysis and writing of CSV files.Apache Commons CSV's core functions and APIs make the CSV file operation more convenient and flexible.The core function and API of the Apache Commons CSV framework will be introduced in detail below.
1. CSVPARSER class
The CSVPARSER class is one of the core categories of the Apache Commons CSV framework, which is used to analyze the CSV file.It provides a number of constructed methods that support different CSV formats, which can customize separators, quotes characters, and rotary characters.The following is a sample code using CSVPARSER:
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
try (CSVParser parser = CSVParser.parse(csvFile, Charset.defaultCharset(), CSVFormat.DEFAULT)) {
for (CSVRecord record : parser) {
String value1 = record.get (0); // Get the value of the first column
String value2 = record.get (1); // Get the value of the second column
// Process record data
}
} catch (IOException e) {
// Treatment analysis abnormalities
}
This code uses the CSVPARSER.PARSE () method to resolve the CSV file into a CSVPARSER object, and then traverse each record and use the .get () method to obtain the specified column value.
2. CSVPrinter class
The CSVPrinter class is another core class of the Apache Commons CSV framework, which is used to write data into the CSV file.It provides multiple methods for formatting CSV data and writing it into files.The following is an example code using CSVPrinter:
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
try (CSVPrinter printer = new CSVPrinter(new FileWriter("output.csv"), CSVFormat.DEFAULT)) {
printer.printRecord ("Value1", "Value2", "Value3"); // Write a record
Printer.printRecord (Arrays.aslist ("Value4", "Value5", "Value6"); // Write another record
// Add more records
} catch (IOException e) {
// Treatment writing abnormalities
}
This code uses CSVPrinter to write the data into a file named "OUTPUT.CSV".You can write a record through the .printrecord () method, which can be directly passed into the string as a parameter, or a list can also be passed.
3. CSVFormat class
The CSVFormat class defines the format of the CSV file in the Apache Commons CSV framework.It provides multiple pre -defined formats, such as DEFAULT, Excel, and TDF.If you need to customize the format, you can also use the csvformat.custom () method to customize.The following is an example code using CSVFormat:
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
Csvformat.custom (). Withheader ("column1", "column2", "column3") // Specify CSV header
.withdelimiter (',') // Specify the separator
.withRecordSeparator("\r
") // Specify the recording separator
.parse (Reader); // Pay CSV files through CSVPARSER
Csvformat.excel.withnullstring ("null") // Specify the empty value string
.withfirstRecordasheader () // Use the first line as the head
.print (writer); // Write the CSV file by CSVPrinter
This code customizes a CSV format through the csvformat.custom () method, specifying the heads, separators, and recording separators.Use CSVFormat.excel to use a predetermined format and set options such as empty value string and head.
Summarize:
Through the core class CSVParser, CSVPrinter, and CSVFormat of the core class of the Apache Commons CSV framework, we can easily handle the CSV file.CSVPARSER is used to analyze CSV files, CSVPrinter is used to write data to CSV files, and CSVFormat is used to define the format of CSV files.Through flexible API and rich options, we can easily read and write CSV files.
I hope this article will help you understand the core function and API of the Apache Commons CSV framework.