HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / I/O / Java Read CSV File Example

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
Sourcecode Download

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. ugur

    July 15, 2020

    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.

  2. Joost de Folter

    March 27, 2020

    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.

  3. Rajat

    October 24, 2019

    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?

  4. Saravanan

    July 30, 2019

    Can we directly replace line1 in file without reading of other lines in CSV file ?

  5. Bala

    August 8, 2018

    Hi sudha,

    Did you complete your requirement.

  6. Y.K

    July 13, 2015

    //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();

    }

    }

  7. Y.K

    July 13, 2015

    // 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;
    }

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

    }

  8. Jerry Bakster

    May 3, 2015

    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();
    }

  9. Saurav

    September 6, 2014

    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

  10. Ravi varma elthepu

    August 11, 2014

    I want to use Apache poi to generate .csv file,as am new to software field,i need some Example code snippets.

    • Lokesh Gupta

      August 11, 2014

      Apache POI is mainly used for reading/writing excel files. Definitely, you can tweak your code to generate CSV with some dirty tricks.
      I will recommend to use OpenCSV. It’s much better.

      • krishna induri

        August 18, 2014

        hi Lokesh,open CSV is a customer friendly version.
        In real time, developers/programmers would not be preferred to use friendly versions. am I right?

        • Lokesh Gupta

          August 18, 2014

          Why so? I do not see any difficulty in including a mature product with friendly version. Perhaps I am missing something that you are more aware of. Please share/elaborate.

  11. Venki

    July 7, 2014

    How to read the csv file from ftp server, Can you provide sample code.

  12. Felix Ekwueme

    May 15, 2014

    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)

    • Lokesh Gupta

      May 19, 2014

      I believe you tried to post some code. Please re-post in [xml]…..[/xml] tags. And please let me know if you already tried anything.

  13. Rahul Gupta

    March 20, 2014

    @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.

  14. Shael

    February 18, 2014

    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..

    • Lokesh Gupta

      February 18, 2014

      Sorry, I don’t have to write from scratch. If you have tried anything, and struck anywhere I can help you.

  15. manoj

    November 29, 2013

    full java code for creating .csv file by fetching data from database like name,id,email

  16. jackline

    November 22, 2013

    you can read, wrote, modify and even convert CSV and excel files using Aspose.Cells for Java:

    https://www.aspose.com/java/excel-component.aspx

  17. Deb

    November 4, 2013

    how can i read a data from .txt file and store it in a string using java.please advise little urgent.

    • Lokesh Gupta

      November 5, 2013

      https://howtodoinjava.com/apache-commons/how-to-read-data-from-inputstream-into-string-in-java/

  18. Banu

    October 26, 2013

    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?

    • Lokesh Gupta

      October 26, 2013

      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.

  19. Sudhamani

    October 9, 2013

    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

    • Lokesh Gupta

      October 9, 2013

      Hi Sudha, I am sorry. I am not the right guy who can help you in this problem. Try googling.

Comments are closed on this article!

Search Tutorials

Java IO

  • Java IO Introduction
  • Java How IO works?
  • Java IO vs NIO
  • Java Create File
  • Java Write to File
  • Java Append to File
  • Java Read File
  • Java Read File to String
  • Java Read File to Byte[]
  • Java Make File Read Only
  • Java Copy File
  • Java Copy Directory
  • Java Delete Directory
  • Java Current Working Directory
  • Java Read/Write Properties File
  • Java Read File from Resources
  • Java Read File from Classpath
  • Java Read/Write UTF-8 Data
  • Java Check if File Exist
  • Java Create Temporary File
  • Java Write to Temporary File
  • Java Delete Temporary File
  • Java Read from Console
  • Java Typesafe input using Scanner
  • Java Password Protected Zip
  • Java Unzip with Subdirectories
  • Java Generate SHA/MD5
  • Java Read CSV File
  • Java InputStream to String
  • Java String to InputStream
  • Java OutputStream to InputStream
  • Java InputStreamReader
  • Java BufferedReader
  • Java FileReader
  • Java LineNumberReader
  • Java StringReader
  • Java FileWriter
  • Java BufferedWriter
  • Java FilenameFilter
  • Java FileFilter

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces