Introduction to the "CSV" framework in the Java class library
CSV (COMMA-SEPARATED VALUES) is a commonly used file format that is usually used to transmit and store data between different applications.There are many frameworks in the Java library that can be used to process CSV files. These frameworks provide simple and flexible methods to read, write and operate data in the CSV file.This article will introduce some common Java CSV frameworks and provide some example code.
1. OpenCSV framework:
OpenCSV is a popular Java CSV framework that provides the function of reading, writing and analyzing CSV files.It uses simple and easy -to -understand API to easily handle CSV files.
1.1. Read CSV file:
Below is an example code that reads CSV file data using the OpenCSV framework:
CSVReader reader = new CSVReader(new FileReader("data.csv"));
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
for (String cell : nextLine) {
System.out.print(cell + " ");
}
System.out.println();
}
reader.close();
1.2. Write to CSV file:
Below is a sample code written to the CSV file data using the OpenCSV framework:
CSVWriter writer = new CSVWriter(new FileWriter("data.csv"));
String[] row1 = {"John", "Doe", "25"};
String[] row2 = {"Jane", "Smith", "30"};
writer.writeNext(row1);
writer.writeNext(row2);
writer.close();
2. Apache Commons CSV framework:
Apache Commons CSV is a powerful Java CSV framework that provides the function of reading, writing and analyzing CSV files, and supports various CSV formats.
2.1. Read CSV file:
Below is a sample code that reads CSV file data with Apache Commons CSV framework:
Reader reader = Files.newBufferedReader(Paths.get("data.csv"));
CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT);
for (CSVRecord record : parser) {
for (String cell : record) {
System.out.print(cell + " ");
}
System.out.println();
}
parser.close();
2.2. Write to CSV file:
Below is a sample code written to CSV file data with Apache Commons CSV framework:
Writer writer = Files.newBufferedWriter(Paths.get("data.csv"));
CSVPrinter printer = new CSVPrinter(writer, CSVFormat.DEFAULT);
printer.printRecord("John", "Doe", "25");
printer.printRecord("Jane", "Smith", "30");
printer.close();
3. Super CSV framework:
Super CSV is another popular Java CSV framework, which provides the function of reading, writing, analysis, and conversion of CSV file data.The Super CSV is more convenient to conversion between CSV files and Java objects by annotating and model -driven.
3.1. Read CSV file:
Below is an example code that reads CSV file data using the Super CSV framework:
ICsvBeanReader beanReader = new CsvBeanReader(new FileReader("data.csv"), CsvPreference.STANDARD_PREFERENCE);
String[] header = beanReader.getHeader(true);
Person person;
while ((person = beanReader.read(Person.class, header)) != null) {
System.out.println(person);
}
beanReader.close();
3.2. Write to CSV file:
Below is an example code that uses the Super CSV framework to write to the CSV file data:
ICsvBeanWriter beanWriter = new CsvBeanWriter(new FileWriter("data.csv"), CsvPreference.STANDARD_PREFERENCE);
String[] header = {"firstName", "lastName", "age"};
beanWriter.writeHeader(header);
beanWriter.write(new Person("John", "Doe", 25), header);
beanWriter.write(new Person("Jane", "Smith", 30), header);
beanWriter.close();
Summary: The CSV framework in the Java class library provides a convenient way to process CSV files, which can easily read, write and operate data in the CSV file.This article introduces some common Java CSV frameworks and provides corresponding example code.Choosing a framework suitable for your needs can greatly simplify the work of CSV file processing.