How To Read A CSV File In Java By Example

Reading and Writing CSV files is a very common task that needs to be performing in java, and very common in IT in general. A CSV files is a very simple format for data files that can be easily exchanged between systems.

The simplicity of the format makes it very easy for all types of environments to be able to read the file. So for example if you have your trading system and you need to send some reconciliation data to your accounting system, a simple way to do that would be to generate a CSV file in your trading system, and send that to your accounting system.

Of course that data will need to be converted into the format required for the accounting system, but first we need to read the data in, and there are a number of options to do that.

What Is A CSV File?

As mentioned in the video, a CSV or Comma Separated Values file, is rows of data delimited by commas. This is the most simple form of CSV that you will see. Sometimes you can have issues with CSV files, especially if the data itself uses the same characters within the data that you are using as the delimiter.

For example if you have a name files and a comma is used to split the name, this could get treated as an additional data and mess up the formatting of your data. In that case what is typically done is to surround the data for each field with quotes so that embedded delimiters can be ignored.

Read A CSV File Using BufferedReader and String.split()

Heres a simple junit test that shows how to read a CSV file and parse the comma seperated values. This would really only be ideal if you knew the format of the input CSV file was going to be guarantee to have no embedded commas

public class CsvTest {

    File csvFile = new File("csvFile.csv");

    @Before
    public void before() throws IOException {
	FileUtils.writeStringToFile(csvFile, "1,Tom,Australia\n", "UTF-8", true);
	FileUtils.writeStringToFile(csvFile, "2,Dick,United Kingdom\n", "UTF-8", true);
	FileUtils.writeStringToFile(csvFile, "3,Harry,USA\n", "UTF-8", true);
    }

    @After
    public void after() {
	csvFile.delete();
    }

    @Test
    public void csvTest() throws Exception {

	String line = "";
	try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {

	    while ((line = br.readLine()) != null) {
		// use comma as separator
		String[] customer = line.split(",");
		System.out.println("Id:" + customer[0] + " , name:" + customer[1] + " , country:" + customer[2] + "]");
	    }

	} catch (IOException e) {
	    e.printStackTrace();
	}

    }
}

Lets break down what we are doing in the various parts of the test

File csvFile = new File("csvFile.csv");

First we setup our File class with the name of our CSV file.


    @Before
    public void before() throws IOException {
	FileUtils.writeStringToFile(csvFile, "1,Tom,Australia\n", "UTF-8", true);
	FileUtils.writeStringToFile(csvFile, "2,Dick,United Kingdom\n", "UTF-8", true);
	FileUtils.writeStringToFile(csvFile, "3,Harry,USA\n", "UTF-8", true);
    }

    @After
    public void after() {
	csvFile.delete();
    }

For the purposes of our unit test we want to setup some simple CSV test data ourselves. To make it simple we use an appache utility library called commons-io that includes the FileUtils package that will let us write some data easily to the file. So here in a Junit before() method we populate our CSV file with 3 CSV records. In the after() method we delete the file to ensure that after each test method we clean up the file.

	String line = "";
	try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {

In the csvTest() method, first we create a String to hold a row of CSV data.

Then we create a FileReader class (reads characters from a file) and a BufferedReader class (buffers the read from the inputstream to make it more efficient). We do this in a try/catch in case an IOException occurs.

	    while ((line = br.readLine()) != null) {

The next line we see if a while statement that is controlling a block of code. This while statement will first read a line of text from the BufferedReader. The read populates the line String, and if that isnt a null value, then the block of code will be executed, and then the while statement will run again. If it is null it means theres no more data to read and so it will exit the while statement and continue on past the while code block.

		// use comma as separator
		String[] customer = line.split(",");
		System.out.println("Id:" + customer[0] + " , name:" + customer[1] + " , country:" + customer[2] + "]");

In our while block of code, we use the String.split() method to split the String into a String array. Then we print out the customer details. If we run our unit test we get the result shown below.

Id:1 , name:Tom , country:Australia]
Id:2 , name:Dick , country:United Kingdom]
Id:3 , name:Harry , country:USA]

Leave a Comment

Your email address will not be published. Required fields are marked *