Java Read CSV File Example

A CSV file is used to store tabular data in plain-text form. To identify and separate different data tokens in the file, a delimiter is used.

CSV files are used by consumers, businesses, and scientific applications. Among its most common uses is moving tabular data between programs that natively operate on incompatible formats. This works because so many programs support some variation of CSV at least as an alternative import/export format.

In Java, there are multiple ways of reading and parsing CSV files.

Table of Contents

1. OpenCSV Library
2. java.util.Scanner
3. String.split() function

1. OpenCSV Library

We can use a robust specialized 3rd party library for reading a CSV file. These libraries give better control on handling the input CSV file. OpenCSV is one such brilliant library. It has following features:

  • Reading arbitrary numbers of values per line
  • Ignoring commas in quoted elements
  • Handling entries that span multiple lines
  • Configurable separator and quote characters
  • Read all the entries at once, or use an Iterator style model

Example 1: Reading a CSV file in Java with OpenCSV

In given example, we are using CSVReader class which wraps a FileReader for reading the actual CSV file. The file is using the delimiter comma.

Using the reader.readNext(), we read the CSV file line by line. When all the lines are read, readNext() method returns null and the program terminates.

package com.howtodoinjava.csv.demoOpenCSV;

import java.io.FileReader;
import java.io.IOException;
import au.com.bytecode.opencsv.CSVReader;

public class OpenCSVExample {
	
	public static void main(String[] args) 
	{
		CSVReader reader = null;
		try 
		{
			//Get the CSVReader instance with specifying the delimiter to be used
			reader = new CSVReader(new FileReader("SampleCSVFile.csv"), ',');

			String [] nextLine;

			//Read one line at a time
		    while ((nextLine = reader.readNext()) != null) 
		    {
		    	for(String token : nextLine)
				{
					//Print all tokens
					System.out.println(token);
				}
		    }
		}
	    catch (Exception e) {
			e.printStackTrace();
		}
		finally	{
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

Program Output:

1
Lokesh
Gupta
howtodoinjava.com
enabled
2
Rakesh
Gupta
howtodoinjava.com
enabled

..More output will appear here

2. java.util.Scanner

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

Example 2: Reading a CSV file in Java using Scanner

package com.howtodoinjava.csv.demoScanner;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ScannerExample 
{
	public static void main(String[] args) throws FileNotFoundException 
	{
		//Get scanner instance
		Scanner scanner = new Scanner(new File("SampleCSVFile.csv"));
		
		//Set the delimiter used in file
		scanner.useDelimiter(",");
		
		//Get all tokens and store them in some data structure
		//I am just printing them
		while (scanner.hasNext()) 
		{
			System.out.print(scanner.next() + "|");
		}
		
		//Do not forget to close the scanner  
		scanner.close();
	}
}

Program Output:

1|Lokesh|Gupta|howtodoinjava.com|enabled
2|Rakesh|Gupta|howtodoinjava.com|enabled
3|Manoj|Sharma|howtodoinjava.com|enabled
4|Abhay|Dwivedi|howtodoinjava.com|enabled
5|John|Adward|howtodoinjava.com|enabled
6|Steve|Jobs|howtodoinjava.com|disabled
7|Bill|Gates|howtodoinjava.com|enabled
8|Tom|Hanks|howtodoinjava.com|enabled
9|Dev|Patel|howtodoinjava.com|disabled

3. String.split()

String.split() function create tokens from the given string into tokens based on provided delimiter as parameter.

It is useful for small strings or small file content.

Example 3: Java Read CSV file with String.split()

In the given example, we are reading a file line by line. Then each line is split into tokens with delimiter comma.

package com.howtodoinjava.csv.demoSplit;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class SplitterExample
{
	public static void main(String[] args)
	{
		//Input file which needs to be parsed
		String fileToParse = "SampleCSVFile.csv";
		BufferedReader fileReader = null;
		
		//Delimiter used in CSV file
		final String DELIMITER = ",";
		try 
		{
			String line = "";
			//Create the file reader
			fileReader = new BufferedReader(new FileReader(fileToParse));
			
			//Read the file line by line
			while ((line = fileReader.readLine()) != null) 
			{
				//Get all tokens available in line
				String[] tokens = line.split(DELIMITER);
				for(String token : tokens)
				{
					//Print all tokens
					System.out.println(token);
				}
			}
		} 
		catch (Exception e) {
			e.printStackTrace();
		} 
		finally 
		{
			try {
				fileReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

Program Output:

1
Lokesh
Gupta
howtodoinjava.com
enabled
2
Rakesh
Gupta
howtodoinjava.com
enabled

..More output will appear here

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

27 thoughts on “Java Read CSV File Example”

  1. Hi, Thank you for your sharing.

    I have a little problem with scanner.useDelimiter(",");. I want to delimeter without inside the ","
    For example: aa,"a,b",cc I want to separate this line such as aa"a,b"cc with using user delimeter . May you give me a suggestion for it.

    Reply
  2. As much as this may work, only option 3: Using a dedicated library like OpenCSV / apache common csv, is the only reliable method for interpreting csv files.

    Reply
  3. My CSV (.csv) file contains data in columns as .xls contains data. So, there is no comma in between values. The values are in columns under their header. How would I read that .csv file by passing header and that should return and the values under the header?

    Reply
  4. //class number 2 – output is list of strings. works for me .

    import java.util.* ;
    import java.io.*;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class FileOut {
    //———————————————————————————
    static public String DateValid( String yk) //time stap evaluation
    {
    Integer me_month;
    Integer me_day;
    int t1_month= 1;
    int t1_day=1;
    String DOB=”-“;
    try{ Integer q= Integer.valueOf(yk); } catch( java.lang.NumberFormatException q ) { DOB=””; };
    if( yk == null || DOB == “” ) DOB = “”;
    else if ( yk.length() != 8 || DOB == “” ) DOB = “”;

    if( DOB != “” )
    {
    me_month = Integer.valueOf( yk.substring(4,6) );
    t1_month = me_month.intValue();

    me_day = Integer.valueOf( yk.substring(6,8) );
    t1_day = me_day.intValue();

    if ( t1_month 0 ) //t1_day 0)
    {
    if ( ( t1_month == 1 || t1_month == 3 || t1_month == 5 || t1_month == 7 || t1_month == 8 || t1_month == 10 || t1_month == 12 ) && ( t1_day 0 ) )
    DOB = yk.substring(4,6) + “/” + yk.substring(6,8) + “/” + yk.substring(0,4);
    else if
    ( ( t1_month == 4 || t1_month == 6 || t1_month == 9 || t1_month ==11 ) && ( t1_day 0 ) )
    DOB = yk.substring(4,6) + “/” + yk.substring(6,8) + “/” + yk.substring(0,4);
    else if
    ( ( t1_month == 2) && ( t1_day 0 ) )
    DOB = yk.substring(4,6) + “/” + yk.substring(6,8) + “/” + yk.substring(0,4);
    else
    DOB = “”;

    }

    else
    {
    DOB = “”;
    }

    }
    return DOB;

    };
    //—————————————————————————————

    public static void main(String[] args) throws FileNotFoundException ,IOException {

    String[] splitStrings;
    StringConvert mar = new StringConvert( 120 );
    String DOB = “-“;
    String MY_ZIP = “”;
    String DOG = “”;
    String Term = “”;
    int qwer = 0;
    Integer me_month;
    Integer me_day;
    int t1_month= 1;
    int t1_day=1;
    String Phone;
    String Mobile;
    String Interest=””;
    String Email = “LOP”;
    String HSGrad = “”;
    int foo ;
    int rt=0;
    //———————— File write —————————————————–

    int firstLine = 0;
    File writeFile = new File(args[0] + “.txt”);
    BufferedWriter br = new BufferedWriter(new FileWriter(writeFile));

    //————————- File read——————————————————-

    File readFile = new File(args[0]);
    Scanner in = new Scanner (readFile);
    //————————-Work with Files write title————————————–

    //br.write( “Email|FirstName|MiddleName|LastName|Suffix|Gender|DateofBirth|Address1|Address2|Address3|City|State|PostalCode|Country|Phone|Mobile|SendSMS|CEEBCODE|HSGradDate|LeadSource|LeadSourceComment|AcademicInterest|StudentType|StartTermCode” );
    //br.newLine();

    //————————-Work with Files write body—————————————
    while( in.hasNextLine() )
    {
    String line = in.nextLine();
    mar.setAbc(line);
    splitStrings = mar.SubstituteCommas();

    if(firstLine != 0)
    {
    //————————————————————————————————-

    if( splitStrings[14].equals(“F”) )
    splitStrings[14] = “Female”;
    else if ( splitStrings[14].equals(“M”) )
    splitStrings[14] = “Male”;
    else
    splitStrings[14] = “”;

    //–==================================================================================================================

    SimpleDateFormat formatter = new SimpleDateFormat(“mm/dd/yyyy”);
    //String dateInString = splitStrings[15];
    try {

    Date date = formatter.parse(splitStrings[15]);
    Date date_1 = formatter.parse(“01/01/1920”);
    //DOB = splitStrings[15];

    if(date.compareTo(date_1)>0)
    {
    DOB = splitStrings[15];

    }
    else
    {
    DOB = “”;
    }

    }
    catch (ParseException e) {
    //e.printStackTrace();
    DOB = “”;
    }

    //===================================================================================================================

    //–==================================================================================================================

    if( splitStrings[26].trim().length() > 253 )
    {

    Interest = splitStrings[26].trim().substring(0,254) ;
    }
    else
    {

    Interest = splitStrings[26].trim();
    }

    //————————————————————————————————-
    //—-checks HSGrad year starts—————————————————————–
    try {
    foo = Integer.parseInt( splitStrings[39] );
    //System.out.println(foo);

    if ( foo > 2008 && foo 2 )

    { //if email double check start
    // email // first name // MIDDLE NAME //Last name //suffix //gender
    br.write( splitStrings[23] + “|” + splitStrings[3] + “|” + splitStrings[4] + “|” + splitStrings[5] + “|” + “|” + splitStrings[14]

    //DOB //Address1 //Address2 //Address3 //City //State
    + “|” + DOB + “|” + splitStrings[6] + “|” + splitStrings[7] + “|” + “|” + splitStrings[8] + “|” + splitStrings[11]

    //postalcode //country //phone
    + “|” + splitStrings[12] + “|” + splitStrings[13] + “|” + splitStrings[20] +

    //mobile //sendssms //CEEBCODE
    “|” + splitStrings[21] + “|” + splitStrings[22] + “|” + splitStrings[36] + “|” + HSGrad + “|”

    //leadsource //leadsourcecomment //academicinterest //studenttype //term
    + “NACAC” + “|” + args[0] + “|” + Interest + “|” + “Freshman” + “|” + Term

    // veteran //EventID //GroupTotal
    + “|” + “|” + “|” + “|”

    + “|” + “|” + “|” + “|” + “|”

    + “|” + “|” + “|” + “|” + “|”

    + “|” + “|” + “|” + “|”

    );

    br.newLine();
    } // if email double check end
    Email = splitStrings[23];
    }
    firstLine=1;

    }

    in.close();
    br.close();

    }

    }

    Reply
  5. // 2 classes – output list of string .each string with particular index refers column with the same index
    // this is the first class

    import java.util.* ;

    public class StringConvert {

    public String abc;
    private Integer i;
    public String[] words;

    StringConvert( Integer i ){ this.i = i; this.words = new String[i]; };
    StringConvert(String abc , Integer i ) { this.abc = abc; this.i = i; this.words = new String[i]; };

    public String toString(){ return abc;};

    //————————————————————————–
    public Integer getI() { return i;}

    public void setI(Integer i) {this.i = i;}

    public String getAbc() {return abc;}

    public void setAbc(String abc) { this.abc = abc;}
    //————————————————————————–

    public String[] SubstituteCommas()
    {

    int trig1 = 0;
    int trig2 = 0;
    char[] charArray ;
    charArray = abc.toCharArray();
    String[] myArray = new String[i];
    for ( int y=0; y < abc.length() ; y++ )
    {

    trig2 = 0;
    if( charArray[y] == '\"' && trig1 ==0 )
    { trig1 =1; trig2 = 1; }

    if( charArray[y] == '\"' && trig1 ==1 && trig2 ==0 )
    { trig1 =0; }

    if ( trig1==1 && charArray[y] == ',' )
    charArray[y]='^';

    }

    //———————

    //abc = String.valueOf(charArray);

    //———————
    int me =0;
    int prev = 0;
    int trig = 0;
    for ( int y=0; y < abc.length() ; y++ )
    {
    trig = 0;

    if ( charArray[y] ==',' && prev == 0 )
    {
    words[me] = abc.substring(prev, y );
    words[me] = getRidQuoters(words[me]);
    prev= y;
    me++;
    trig = 1;

    }

    if ( charArray[y] ==',' && prev != 0 && trig == 0 )
    {
    words[me] = abc.substring(prev + 1, y );
    words[me] = getRidQuoters(words[me]);
    prev= y;
    me++;

    }

    if ( y == abc.length() – 1 && prev != 0 && trig == 0 )
    {
    words[me] = abc.substring(prev + 1, y + 1 );
    words[me] = getRidQuoters(words[me]);
    prev= y;
    me++;

    }

    }

    return words;

    }

    //———————————————————————————————
    public String getRidQuoters( String abc )
    {
    char[] charArray ;
    charArray = abc.toCharArray();

    for ( int y=0; y < abc.length() ; y++ )
    {
    if ( charArray[y] == '"' )
    charArray[y] = ' ';

    if ( charArray[y] == '|' )
    charArray[y] = ' ';

    }
    abc = String.valueOf(charArray);
    abc = abc.trim();

    return abc;
    }

    //———————————————————————————————

    }

    Reply
  6. Thanks for the article, and I’m sharing one more open-source library for reading/writing/mapping CSV data. Since I used this library in my project, I found out it powerful and flexiable especially parsing big CSV data (such as 1GB+ file or complex processing logic).

    The library provides simplified API for Java developers, and also it provided full features in parsing CSV file.

    Here is a code snippt for using this library:

    public static void main(String[] args) throws FileNotFoundException {
    /**
    * ---------------------------------------
    * Read CSV rows into 2-dimensional array
    * ---------------------------------------
    */
    // 1st, config the CSV reader, such as line separator, column separator and so on
    CsvParserSettings settings = new CsvParserSettings();
    settings.getFormat().setLineSeparator("n");

    // 2nd, creates a CSV parser with the configs
    CsvParser parser = new CsvParser(settings);

    // 3rd, parses all rows from the CSV file into a 2-dimensional array
    List resolvedData = parser.parseAll(new FileReader("/examples/example.csv"));

    /**
    * ---------------------------------------------
    * Read CSV rows into list of beans you defined
    * ---------------------------------------------
    */
    // 1st, config the CSV reader with row processor attaching the bean definition
    BeanListProcessor rowProcessor = new BeanListProcessor(ColumnBean.class);
    settings.setRowProcessor(rowProcessor);
    settings.setHeaderExtractionEnabled(true);

    // 2nd, parse all rows from the CSF file into the list of beans you defined
    parser.parse(new FileReader("/examples/example.csv"));
    List resolvedBeans = rowProcessor.getBeans();
    }

    Reply
  7. There are two csv files.I want to read a csv file and match the parameter of file1.csv with file2.csv.The matching rows should be in the output file..How do we do it..Please help

    Reply
  8. How do I read data from a csv and excel file to generate an xml file using java? Example:
    “tests.csv”, and “parameter.xlsx” to generate “Automation.xml”.

    Example:

    If I want to read an attribute “Plays” to find the parameters associated it, I type “Plays”, in the program and it searches for “Plays” under the “tests.csv” and “parameter.xlsx” files and generates a parameter such as “hamlet”. or If I want to see an attribute and the parameters associated with “MichaelJackson”, I type “MichaelJackson”, in the program and it searches for “MichaelJackson” under the “tests.csv” and “parameter.xlsx” files and generates parameters such as “yourockmyworld” and ‘thriller”.

    If there is a parameter associated with “plays” in the “parameter.xlsx” file, the program returns an xml file containing the information, and if there are more than one parameter, it lists the second parameter as well (e.g “MichaelJackson”). If there is no parameter for “Plays”, it returns no result.

    Sample csv file: (//this is not a csv file…using this to show the location of the attribute in regards to hierarchy)

    BookStageCategoryPlays;
    PopStageArtistsMichaelJackson

    Sample Excel file:
    Attribute Parameters
    Plays hamlet
    MichaelJackson yourockmyworld, thriller

    Sample XML result expected: (//Disregard the asterisks)

    Reply
  9. @Lokesh : There is one point to be noted here, in scanner example, I have to mention path also as src/SampleCSVFile.csv. When I have pasted your code as it is, it would prompted me error as FileNotFoundException.

    Reply
  10. i have a simple program which fetch the data from a notepad called “employees.txt”
    inside is:

    EMP-001 , Paul Flores, DBA , 500
    EMP-002 , Rio Reyes , PROG , 450
    EMP-003 , Kate Smith , PROG , 450
    EMP-004 , Suzanne Moretz , PROG , 300

    now, i want to fetch the record. if i typed EMP-001, it will select the row from it, and the output must be,

    EMPLOYEE______POSITION_____RATE PER HOUR
    Paul Flores ———— DBA ——————–500

    its a simple program but i cant get it right.. please give the codes..

    Reply
  11. Hi Lokesh,

    I need to read the excel data and construct graphs based on it. i dont want use any database. I have a suggestion of converting the excel to json first. then using jqplot,i will construct the graphs. i am struck with json construction from excel. Can u provide suggestion?

    Reply
    • Never worked on such requirement. But I think a good solution would be as simple as reading data into java objects and then convert them to json. I will appreciate if you share your final decision here. It will benefit others also.

      Reply
  12. Hi I am SudhaManju from India… I am working in a project on duplicate code detection in java…..
    I want read a software and need to find duplicate code present in that.. I need to count the number of loops and functions present in a software…… I am beginner .. so can help me to read a code file in java and compare line by line….

    my email id :
    pls mail your suggestion to my id…….

    thank you

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.