1. Brief description
In Java development, CSV (Comma-Separated Values) is a common data storage format that is widely used in data exchange and simple storage tasks. Apache Commons CSV is a lightweight library provided by Apache, focusing on simplifying the parsing and generation of CSV files, and supports a variety of CSV formats, such as Excel, RFC 4180, MySQL, etc.
This article will introduce the core capabilities of Commons CSV and demonstrate its powerful capabilities in CSV file parsing and generation through several detailed usage examples.
2. Why choose Commons CSV?
- Lightweight: No huge dependencies required, centralized functions.
- Supports multiple formats: compatible with Excel, RFC 4180, Tab-separated formats.
- Simple and easy to use: The API is designed with clear and easy to use.
- High flexibility: supports various configurations such as custom delimiters, custom line breaks, etc.
Before using Commons CSV, it is necessary to add its dependencies. Here are the Maven dependencies for Commons CSV:
<dependency> <groupId></groupId> <artifactId>commons-csv</artifactId> <version>1.10.0</version> </dependency>
3. Use examples
Common examples of Spring Boot integrated Commons CSV, the following examples are for reference:
3.1 Write to CSV files
Commons CSV also supports easy generation of CSV files:
package ; import ; import ; import ; import ; public class CsvWriterExample { public static void main(String[] args) throws IOException { // Create a CSV file try (FileWriter writer = new FileWriter("e:\\csv\\"); CSVPrinter printer = new CSVPrinter(writer, .withHeader("ID", "Name", "Age", "Email"))) { ("1", "Alice", "25", "alice@"); ("2", "Bob", "30", "bob@"); ("3", "Charlie", "35", "charlie@"); } } }
3.2 Using custom delimiters
If you need a custom delimiter (such as semicolon:), you can do it through configuration:
package ; import ; import ; import ; public class CustomDelimiterExample { public static void main(String[] args) throws Exception { try (FileWriter writer = new FileWriter("e:\\csv\\custom_delimiter.csv"); CSVPrinter printer = new CSVPrinter(writer, .withHeader("ID", "Name", "Age", "Email") .withDelimiter(':'))) { ("1", "Diana", "40", "diana@"); ("2", "Eve", "22", "eve@"); } } }
3.3 Parsing nested quotes or special characters
CSV files may contain nested quotes or special characters (such as line breaks), and Commons CSV can easily parse:
package ; import ; import ; import ; public class SpecialCharacterExample { public static void main(String[] args) throws Exception { String csvData = "ID,Name,Notes\n" + "1,\"John\",\"Loves coding\nand teaching\"\n" + "2,\"Jane\",\"Enjoys reading\""; try (CSVParser parser = .withFirstRecordAsHeader() .parse(new StringReader(csvData))) { (record -> { String id = ("ID"); String name = ("Name"); String notes = ("Notes"); ("ID: %s, Name: %s, Notes: %s%n", id, name, notes); }); } } }
3.4 Using Enumeration Mapping Fields
For CSV files with well-defined fields, you can use enumerations to avoid hard-coded field names:
package ; import ; import ; import ; import ; public class EnumFieldExample { enum Header { ID, Name, Age, Email } public static void main(String[] args) throws Exception { try (FileReader reader = new FileReader("e:\\csv\\"); CSVParser parser = .withFirstRecordAsHeader() .parse(reader)) { for (CSVRecord record : parser) { String id = (); String name = (); String age = (); String email = (); ("ID: %s, Name: %s, Age: %s, Email: %s%n", id, name, age, email); } } } }
4. Summary
Apache Commons CSV is an efficient tool for processing CSV files, providing a simple and efficient solution whether parsing complex CSV data or generating custom format CSV files.
advantage:
- Lightweight and easy to use.
- Rich functional support, such as custom delimiters and multi-format support.
- Provides comprehensive CSV file reading and writing capabilities.
Applicable scenarios:
- Data import and export.
- Data conversion and cleaning.
- As a lightweight database in your application.
- Through the examples in this article, I hope you can quickly master the usage of Commons CSV and flexibly apply it to actual projects!
The above is the detailed information of Java's operation guide for efficient processing of CSV files using Apache Commons. For more information about Java Apache Commons processing of CSV, please follow my other related articles!