Quick entry: Use Apache Commons CSV to analyze and generate CSV files
Quick entry: Use Apache Commons CSV to analyze and generate CSV files
CSV (comma segmental value) is a commonly used file format that can be used to store and transmit structured data.Apache Commons CSV is a Java library that provides a simple and flexible way to analyze and generate CSV files.This article will introduce how to use the Apache Commons CSV to analyze and generate CSV files.
1. Add Apache Commons CSV dependency library
First, you need to add Apache Commons CSV to the Java project.You can use Maven for dependency management, add the following code to the pom.xml file of the project:
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
2. Analyze CSV file
To analyze the CSV file, you can use the CSVPARSER class.The following is a sample code that demonstrates how to analyze CSV files containing names and age:
import org.apache.commons.csv.*;
public class CSVParserExample {
public static void main(String[] args) throws Exception {
CSVParser parser = new CSVParserBuilder()
.withSeparator(',')
.withQuoteChar('"')
.build();
Reader reader = Files.newBufferedReader(Paths.get("data.csv"));
CSVReader csvReader = new CSVReaderBuilder(reader)
.withskiplines (1) // skip the title line
.withCSVParser(parser)
.build();
String[] line;
while ((line = csvReader.readNext()) != null) {
String name = line[0];
int age = Integer.parseInt(line[1]);
System.out.println ("Name:" + Name + ", age:" + Age);
}
csvReader.close();
reader.close();
}
}
The above code uses CSVPARSER and CSVReader to analyze CSV files.In the example, we set the comma as the separators of the field and use dual quotes as reference characters.By skipping the first line of title line, we can read the name and age data of each line and print it out.
3. Generate CSV file
To generate CSV files, you can use the CSVPrinter class.Here are a sample code that demonstrates how to generate CSV files containing names and age:
import org.apache.commons.csv.*;
public class CSVGeneratorExample {
public static void main(String[] args) throws Exception {
Writer writer = Files.newBufferedWriter(Paths.get("data.csv"));
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT
.withheader ("name", "age");
csvprinter.printoldRcord ("Zhang San", 25);
csvprinter.printoldRcord ("Li Si", 30);
csvPrinter.flush();
csvPrinter.close();
}
}
The above code uses CSVPrinter to generate CSV files.We first create a Writer object to specify the path of the output file.Create a CSVPrinter object and pass in Writer and CSVFormat objects.By calling the PrintRecord method, we can add records to the CSV file.Finally, remember to refresh and close the CSVprinter.
Through the above steps, you can use the Apache Commons CSV library to analyze and generate the CSV file.Whether you extract data from the existing CSV files or write data into the CSV file, Apache Commons CSV provides simple and powerful tools to process CSV files.