Reading CSV Files Using OpenCSV Library
In this step, we will implement our third approach to reading CSV files using the OpenCSV library. OpenCSV is a third-party library that provides robust CSV parsing capabilities, handling complex scenarios like fields containing commas or newlines enclosed in quotes.
Understanding OpenCSV
OpenCSV is a CSV parser library for Java that supports all the basic CSV-format variations. Unlike the previous approaches, OpenCSV properly handles quoted fields containing commas, line breaks, and other special characters that would otherwise break simple splitting by commas.
Setting Up OpenCSV
First, let's download the OpenCSV library and its dependencies:
cd ~/project
mkdir -p lib
curl -L -o lib/opencsv-5.7.1.jar https://repo1.maven.org/maven2/com/opencsv/opencsv/5.7.1/opencsv-5.7.1.jar
curl -L -o lib/commons-lang3-3.12.0.jar https://repo1.maven.org/maven2/org/apache/commons/commons-lang3/3.12.0/commons-lang3-3.12.0.jar
curl -L -o lib/commons-text-1.10.0.jar https://repo1.maven.org/maven2/org/apache/commons/commons-text/1.10.0/commons-text-1.10.0.jar
curl -L -o lib/commons-beanutils-1.9.4.jar https://repo1.maven.org/maven2/commons-beanutils/commons-beanutils/1.9.4/commons-beanutils-1.9.4.jar
curl -L -o lib/commons-collections-3.2.2.jar https://repo1.maven.org/maven2/commons-collections/commons-collections/3.2.2/commons-collections-3.2.2.jar
curl -L -o lib/commons-logging-1.2.jar https://repo1.maven.org/maven2/commons-logging/commons-logging/1.2/commons-logging-1.2.jar
Creating a More Complex CSV File
Let's create a more complex CSV file that includes quoted fields with commas:
echo 'name,description,price
"Laptop","High-performance laptop, with SSD",999.99
"Smartphone","Latest model, with dual camera",499.99
"Headphones","Noise-canceling, wireless",149.99' > ~/project/products.csv
Implementing CSV Reading with OpenCSV
Now, let's update our CSVReaderDemo.java file to read the CSV file using OpenCSV. Replace the entire content of the file with the following code:
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
import java.io.FileReader;
import java.io.IOException;
public class CSVReaderDemo {
public static void main(String[] args) {
System.out.println("Reading CSV using OpenCSV");
// Path to our CSV file with complex data
String csvFile = "products.csv";
try (CSVReader reader = new CSVReader(new FileReader(csvFile))) {
// Read and print the header
String[] header = reader.readNext();
if (header != null) {
System.out.println("\nHeader: " + String.join(", ", header));
}
// Read and print each line
String[] nextLine;
int rowNumber = 1;
System.out.println("\nData read from CSV file:");
while ((nextLine = reader.readNext()) != null) {
System.out.println("Row " + rowNumber + ":");
for (int i = 0; i < nextLine.length; i++) {
System.out.println(" " + header[i] + ": " + nextLine[i]);
}
rowNumber++;
System.out.println();
}
} catch (IOException | CsvValidationException e) {
System.err.println("Error reading the CSV file: " + e.getMessage());
e.printStackTrace();
}
}
}
Let's compile and run our updated code:
cd ~/project
javac -cp ".:lib/*" -d . src/CSVReaderDemo.java
java -cp ".:lib/*" CSVReaderDemo
You should see output similar to this:
Reading CSV using OpenCSV
Header: name, description, price
Data read from CSV file:
Row 1:
name: Laptop
description: High-performance laptop, with SSD
price: 999.99
Row 2:
name: Smartphone
description: Latest model, with dual camera
price: 499.99
Row 3:
name: Headphones
description: Noise-canceling, wireless
price: 149.99
Code Explanation
- We import necessary classes from the OpenCSV library and Java I/O.
- We define the path to our CSV file (
products.csv).
- We create a
CSVReader object to read the CSV file.
- We read the header row with
reader.readNext() and store it for later use.
- We then read each subsequent row with
reader.readNext() in a loop until there are no more rows.
- For each row, we print each field along with its corresponding header.
The OpenCSV library handles the complex CSV formatting automatically, correctly parsing fields with commas enclosed in quotes. This makes it ideal for real-world CSV files that may contain complex data.
Advantages of OpenCSV
OpenCSV offers several advantages over the basic approaches:
- It correctly handles quoted fields containing commas, newlines, and other special characters.
- It provides built-in support for reading into beans (Java objects).
- It supports advanced features like custom separators, quote characters, and escape characters.
- It handles large CSV files efficiently.
For most real-world applications dealing with CSV files, using a dedicated library like OpenCSV is the recommended approach.