Java Read CSV File Example

In the previous tutorial on how to write or create a CSV file, we learned that CSV stands for Comma Separated Value, which means you have a file with rows of columns of data that are separated by a comma. The Apache Commons CSV Library is ideal for to easily read CSV file contents.

In the example below, I am doing development using Eclipse and Maven. The big advantage of a Maven project is that I can quickly add a dependency library to my pom.xml file and I instantly can use the library function that I need.


Just like the write CSV tutorial, I add Apache Common CSV library to my pom.xml file:

    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-csv</artifactId>
      <version>1.5</version>
    </dependency> 


Java Read CSV File Source Code

In our sample code, we start by created a buffered reader object to read the cast.csv file. Next step is to define the format of our file, which we defined as first name, last name, and finally age. We iterate over each record using the enhanced loop and proceed to print the each record to the screen.

package com.jcd.java.tutorials.MavenProject;

import java.io.BufferedReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;

public class JavaReadCSVFile 
{

	public static void main(String[] args) 
	{
		
		//Define the CSV Parser object
		CSVParser jcdCSVParser;
		
		try
		{
		
			//Let's create a BufferedReader object pointing to our csv file
			BufferedReader reader = Files.newBufferedReader(Paths.get("cast.csv"));
			
			//Let's define our CSV Parser with the three column format we defined in the writer
			jcdCSVParser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader("First Name", "Last Name", "Age").withIgnoreHeaderCase().withTrim());
	        
	        for (CSVRecord jcdCSVRecord : jcdCSVParser) 
	        {
	        	
	        	//Let's skip the column header
	        	if(jcdCSVRecord.getRecordNumber() == 1) { continue; }
	        	
	            //Let's grab the column for first name
	            String firstName = jcdCSVRecord.get("First Name");
	            
	            //Let's grab the column for last name
	            String lastName = jcdCSVRecord.get("Last Name");
	            
	            //Let's grab the column for age
	            String age = jcdCSVRecord.get("Age");
	            
	            //Let's print the row out to standard out 
	            System.out.println("First Name: "+firstName);
	            System.out.println("Last Name: "+lastName);
	            System.out.println("Age: "+age);
	            System.out.println();
	        }
	        
	        //Let's close the CSV Parser object
	        jcdCSVParser.close();
	        
		}
		catch(Exception e) 
		{
			System.out.println("Exception: "+e.toString());
		}

	}

}


Java Read CSV File Output

First Name: Fred
Last Name: Flinstone
Age: 95

First Name: Barney
Last Name: Rubble
Age: 90

First Name: Wilma
Last Name: Flinstone
Age: 85

First Name: Betty
Last Name: Rubble
Age: 80


Add a Comment

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