Reading and Writing CSVs in Java

Introduction

This is the first article in a short series dedicated to Libraries for Reading and Writing CSVs in Java.

Reading and Writing CSVs in Core Java

Owning to the popularity and widespread use of CSV as a format for data transfer, there are many parser libraries that can be used along with Java.

Third-party parsers define common formats and are able to work with various delimiters, handle special characters, and sometimes even read non-binary data. However, not all programs require all of those features, so it is still important to be able to handle CSV files with core Java, without the use of any additional libraries.

A simple combination of FileReader, BufferedReader, and String.split() can facilitate reading data from CSVs. Let's consider the steps to open a basic CSV file and parse the data it contains:

  • Use FileReader to open the CSV file
  • Create a BufferedReader and read the file line by line until an "End of File" (EOF) character is reached
  • Use the String.split() method to identify the comma delimiter and split the row into fields
BufferedReader csvReader = new BufferedReader(new FileReader(pathToCsv));  
while ((row = csvReader.readLine()) != null) {  
    String[] data = row.split(",");
    // do something with the data
}
csvReader.close();  

The data String array will contain a list of the fields in each row of the file found in the pathToCsv file location. If the CSV file has a delimiter other than a comma, it can be specified in the split method. For example, another common delimeter is the tab for Tab-Separated-Value (TSV) files.

The data can be passed to a separate method for processing or writing to a database from within the loop, or stored in a Java Collection for later use. For example, if you're writing a large amount of data to a database, constraint violations (like primary key violations, for example) caused by human errors when generating the CSV, can be avoided by using a hash map. If there is a duplicate entry in the CSV, the hash map will store the most recent 'read' and overwrite the previous entry.

Since you're writing the parser from scratch you'll need to take care of simple error handling on your own. Like if you're unsure whether the file exists, it is always safer to enclose the read operation within a try/catch block or add logic to determine the existence of the file before processing.

To check whether the file exists, the following modification can be made to our code:

File csvFile = new File(pathToCsv);  
if (csvFile.isFile()) {  
    // create BufferedReader and read data from csv
}

With some simple error handling and strict requirements on the CSV format, creating a simple parser yourself using core Java components is a task most programmers can take on.

Writing CSVs in Core Java

Most third-party parsers also support writing to CSV files. However, there is a simple way to write to CSV files, just like any other file type, without using any libraries.

The simplest way is to use a FileWriter object and treat the CSV file as any other text file. In our example the data is stored in some List objects, which we just iterate over and append to the writer:

// Our example data
List<List<String>> rows = Arrays.asList(  
    Arrays.asList("Jean", "author", "Java"),
    Arrays.asList("David", "editor", "Python"),
    Arrays.asList("Scott", "editor", "Node.js")
);

FileWriter csvWriter = new FileWriter("new.csv");  
csvWriter.append("Name");  
csvWriter.append(",");  
csvWriter.append("Role");  
csvWriter.append(",");  
csvWriter.append("Topic");  
csvWriter.append("\n");

for (List<String> rowData : rows) {  
    csvWriter.append(String.join(",", rowData));
    csvWriter.append("\n");
}

csvWriter.flush();  
csvWriter.close();  

When using a FileWriter always make sure you flush and close the stream. This improves the performance of the IO operation and indicates that there is no more data to be written to the output stream.

Conclusion

There are several ways to read and write CSV files in Java, the simplest being using Core Java components. Although this also leaves you more vulnerable to bugs and will be less robust than a proven third-party solution.

If you're interested in reading about other ways to read and write CSVs in Java, make sure to check out our other articles:

Author image
Sri Lanka