Java CSV Parsing Example
In this post, we feature a comprehensive Java CSV Parsing Example. CSV stands for Comma Separated Values. As Java developers, we frequently see the need to process CSV files. They are the most commonly used file format for the data interchange. CSV is the simple file format that organizes data in a tabular form and each value is delimited by a comma. CSV can be easily opened in any text editor or in Microsoft Excel for better viewing.
In this article, I am demonstrating CSV file processing using Java. I will cover both reading from the CSV file and writing to the CSV file. The article is divided into different sections,
- Advantages of CSV files
- Development environment
- Reading from a CSV file
- Writing to the CSV file
- Common problems encountered in CSV processing
- Library support
- Download the Source Code
1. Advantages of CSV files
This section lists the requirements to start the first CSV processing examples in Java.
- Easy to read (Simple delimited file with tabular structure)
- Ubiquitously used for data exchange in every field
- Most of the languages support processing of CSV files
- CSV files can be easily loaded to the database
- No special software required to read it
2. Development Environment
- Download and install JDK 8
- Set JAVA_HOME environment variable to current Java installation
- Make sure Java is added in the System Path variable (Windows only)
- User preferred IDE (Eclipse, IntelliJ Idea, NetBeans or any text editors). For this tutorial, I haven’t used any IDE. Instead, programs are created using Notepad++ in Windows 7 operating System
3. Reading from a CSV file
In this section, I will demonstrate a simple program (Without using any external libraries) to read data from the CSV file. Sample input data is as below,
Name, Department, Role, Location Santosh, IT, Developer, Sydney John, HR, Associate, Newyork Lisa, Finance, Senior Accountant, Sydney Mike, HR, Manager, Newyork Catherine, Finance, Accountant, Sydney
Save above data in a file with name as employee-details.csv in the same folder as the .java file.
Java program to read from CSV file,
import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; import java.util.List; public class SimpleCsvReader { public List readCsvFile(String filePath) throws Exception { List employeesList = new ArrayList(); BufferedReader reader = new BufferedReader(new FileReader(filePath)); String line = null; int count = 0; while ((line = reader.readLine()) != null) { //Skip the header row if(count == 0) { count++; continue; } String[] lineContents = line.split(","); EmployeeDetails employeeDetils = new EmployeeDetails(); employeeDetils.setName(lineContents[0]); employeeDetils.setDepartment(lineContents[1]); employeeDetils.setRole(lineContents[2]); employeeDetils.setLocation(lineContents[3]); employeesList.add(employeeDetils); count++; } return employeesList; } public static void main(String[] args) { SimpleCsvReader simpleCsvReader = new SimpleCsvReader(); try { List employeesList = simpleCsvReader.readCsvFile("employee-details.csv"); System.out.println("=========================== Employee Details ===================="); employeesList.stream().forEach((employee) -> { System.out.println("Name = " + employee.getName()); System.out.println("Department = " + employee.getDepartment()); System.out.println("Role = " + employee.getRole()); System.out.println("Location = " + employee.getLocation()); System.out.println("--------------------------------------------------------------"); }); } catch (Exception ex) { ex.printStackTrace(); } } } class EmployeeDetails { private String name; private String department; private String role; private String location; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } }
Steps to create and run the Simple CSV Reader,
- Copy the code and in your folder of choice save it as SimpleCsvReader.java
- Create a CSV file employee-details.csv in the same folder using the data given above
- Go to the command prompt and navigate to the directory where the source code is saved
- Run command
javac SimpleCsvReader.java
, This will compile the program - To run the program run command,
java SimpleCsvReader
in the command prompt
The output from the above program looks as in the picture,
4. Writing to a CSV file
In this section, I will show how to write to a CSV file without using any external libraries. Below is the Java code snippet.
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.util.ArrayList; import java.util.List; public class SimpleCsvWriter { public void writeToFile(String fileName) throws Exception{ List employees = buildEmployeesData(); File csvFile = new File(fileName); FileWriter csvWriter = new FileWriter(csvFile); BufferedWriter bufferedWriter = new BufferedWriter(csvWriter); bufferedWriter.write("Employee Name, Department Name, Role, City"); bufferedWriter.newLine(); for (EmployeeDetails employee : employees) { bufferedWriter.write(employee.getName() + ","); bufferedWriter.write(employee.getDepartment() + ","); bufferedWriter.write(employee.getRole() + ","); bufferedWriter.write(employee.getLocation()); bufferedWriter.newLine(); } csvWriter.flush(); bufferedWriter.close(); csvWriter.close(); } private List buildEmployeesData() { List employees = new ArrayList(); EmployeeDetails employee1 = new EmployeeDetails(); employee1.setName("John"); employee1.setDepartment("Operations"); employee1.setRole("Manager"); employee1.setLocation("Bangalore"); employees.add(employee1); EmployeeDetails employee2 = new EmployeeDetails(); employee2.setName("Stephen"); employee2.setDepartment("Support"); employee2.setRole("Associate"); employee2.setLocation("Bangalore"); employees.add(employee2); return employees; } public static void main(String[] args) { SimpleCsvWriter simpleCsvWriter = new SimpleCsvWriter(); try { simpleCsvWriter.writeToFile("employee-details.csv"); } catch(Exception ex) { ex.printStackTrace(); } } private class EmployeeDetails { private String name; private String department; private String role; private String location; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } } }
Follow the below steps to generate CSV file,
- Copy the program and create the file SimpleCsvWriter.java in the folder of your choice
- Go to command prompt and navigate to the folder
- Run the command
javac SimpleCsvWriter.java
to compile the program - To run the program, run command
java SimpleCsvWriter
- Now if you go to the directory, a file
employee-details.csv
should have been generated with the following content,
Employee Name, Department Name, Role, City John,Operations,Manager,Bangalore Stephen,Support,Associate,Bangalore
5. Common problems encountered in CSV processing
Some of the commonly encountered problems while processing CSV are listed below,
- Data contains the separator (one or more values contain comma itself)
- Header and data columns mismatch (Blank columns)
- Blank rows in between
- Double quotes in the content
With the above simple ways, it is difficult to handle these problematic scenarios. In such instances where complex processing is expected, it is better to use libraries.
6. Library options to process CSV files
- Apache Commons CSV processor – is part of Apache Commons library and provides easy ways to read and write CSV files
- Open CSV – is one of the first commercial-friendly Java CSV processors. It can solve most of the problems with CSV processing mentioned earlier. It supports reading, writing, serializing and deserializing the CSV files.
- Super CSV – is a programmer-friendly and faster CSV processing library. It supports POJO way of mapping CSV data and it is easy to skip blank rows and columns. Also, it is highly configurable in handling special characters.
- Other library options include Skife CSV and Genjava CSV processors
7. Java CSV Parsing – Conclusion
In this article, we have seen the easy ways to process the CSV files without using any external libraries. Java’s file processing APIs can meet most of our basic CSV processing needs. However, libraries will provide more control and feature that could help meet the industry-specific needs.
8. Download the Source Code
You can download the full source code of this example here: Java CSV Parsing Example
Thanks for the very simple explanation. CSV has been around for ages but is still very valid for interfacing large amounts of data more efficiently that any other format ever could. I remember cursing XML… now, many curse JSON (skinny XML). But for simplicity, file size, and transfer speed, nothing beats CSV!