A CSV file is used to store tabular data in plain-text form. A comma delimiter is used to identify and separate different data tokens in the CSV file.
- CSV (Comma Separated Values) files are used by consumers, businesses, and scientific applications. Among its most common uses is moving tabular data between programs in runtime that natively operate on incompatible formats.
- CSV data is popular because so many programs and languages support some variation of CSV at least as an alternative import/export format.
In Java, there are different ways of reading and parsing CSV files. Let us discuss some of the best approaches:
1. Using OpenCSV Library
OpenCSV is a brilliant library for operating on CSV files. It has the 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
Import the latest version of OpenCSV into project dependencies.
<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>2.3</version>
</dependency>
Example 1: Reading the CSV File line by line into String[]
In the given example, we are using CSVReader class from OpenCSV library which wraps a FileReader for reading the actual CSV file. The file uses the delimiter comma.
- Using the
reader.readNext(), we read the CSV file line by line. - It throws IOException if an error occurs in reading the file.
- It throws CsvValidationException if the read line is not a valid CSV string.
- When all the lines are read,
readNext()method returnsnulland the program terminates.
try(CSVReader reader
= new CSVReader(new FileReader("SampleCSVFile.csv")))
{
String [] nextLine;
//Read one line at a time
while ((nextLine = reader.readNext()) != null)
{
//Use the tokens as required
System.out.println(Arrays.toString(nextLine));
}
}
catch (IOException | CsvValidationException e) {
e.printStackTrace();
}
2. Using Super CSV Library
Super CSV is to be the foremost, fastest, and most programmer-friendly, free CSV package for Java. It supports a very long list of useful features out of the box, such as:
- Ability to read and write data as POJO classes
- Automatic encoding and decoding of special characters
- Custom delimiter, quote character and line separator
- Support for cell processors to process each token in a specific manner
- Ability to apply one or more constraints, such as number ranges, string lengths or uniqueness
- Ability to process CSV data from files, strings, streams and even zip files
Add the latest version of the latest version of Super CSV in the project.
<dependency>
<groupId>net.sf.supercsv</groupId>
<artifactId>super-csv</artifactId>
<version>2.4.0</version>
</dependency>
Example 2: Reading the CSV File into POJO
We will read the following CSV file.
CustomerId,CustomerName,Country,PinCode,Email
10001,Lokesh,India,110001,abc@gmail.com
10002,John,USA,220002,def@gmail.com
10003,Blue,France,330003,ghi@gmail.com
The corresponding POJO class is:
public class Customer
{
private Integer CustomerId;
private String CustomerName;
private String Country;
private Long PinCode;
private String Email;
}
Remember that the column names should match up exactly with the bean’s field names, and the bean has the appropriate setters defined for each field.
import java.io.FileReader;
import java.io.IOException;
import org.supercsv.cellprocessor.Optional;
import org.supercsv.cellprocessor.ParseInt;
import org.supercsv.cellprocessor.ParseLong;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.constraint.StrRegEx;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvBeanReader;
import org.supercsv.io.ICsvBeanReader;
import org.supercsv.prefs.CsvPreference;
public class ReadCSVFileExample {
static final String CSV_FILENAME = "data.csv";
public static void main(String[] args) throws IOException
{
try(ICsvBeanReader beanReader
= new CsvBeanReader(new FileReader(CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE))
{
// the header elements are used to map the values to the bean
final String[] headers = beanReader.getHeader(true);
//final String[] headers = new String[]{"CustomerId","CustomerName","Country","PinCode","Email"};
final CellProcessor[] processors = getProcessors();
Customer customer;
while ((customer = beanReader.read(Customer.class, headers, processors)) != null) {
System.out.println(customer);
}
}
}
/**
* Sets up the processors used for the examples.
*/
private static CellProcessor[] getProcessors() {
final String emailRegex = "[a-z0-9\\._]+@[a-z0-9\\.]+";
StrRegEx.registerMessage(emailRegex, "must be a valid email address");
final CellProcessor[] processors = new CellProcessor[] {
new NotNull(new ParseInt()), // CustomerId
new NotNull(), // CustomerName
new NotNull(), // Country
new Optional(new ParseLong()), // PinCode
new StrRegEx(emailRegex) // Email
};
return processors;
}
}
3. Using java.util.Scanner
The Scanner class breaks its input into tokens using a specified delimiter pattern. The default delimiter is whitespace.
- We can use a separate Scanner to read lines, and another scanner to parse each line into tokens. This approach may not be useful for large files because it is creating one scanner instance per line.
- We can use the delimiter comma to parse the CSV file.
- The CSV tokens may then be converted into values of different datatypes using the various
next()methods.
Example 3: Parsing a CSV file using Scanner
try(Scanner scanner = new Scanner(new File("SampleCSVFile.csv"))){
//Read line
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
//Scan the line for tokens
try (Scanner rowScanner = new Scanner(line)) {
rowScanner.useDelimiter(",");
while (rowScanner.hasNext()) {
System.out.print(scanner.next());
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
4. Using BufferedReader and String.split()
In this approach, we use BufferedReader to read the file line by line. Then the String.split() function is used to get tokens from the current line based on provided delimiter as the method parameter.
It is useful for small strings or small files.
Example 4: Splitting the CSV String or CSV File
In the given example, we are reading a file line by line. Then each line is split into tokens with a delimiter comma.
try(BufferedReader fileReader
= new BufferedReader(new FileReader("SampleCSVFile.csv")))
{
String line = "";
//Read the file line by line
while ((line = fileReader.readLine()) != null)
{
//Get all tokens available in line
String[] tokens = line.split(",");
//Verify tokens
System.out.println(Arrays.toString(tokens));
}
}
catch (IOException e) {
e.printStackTrace();
}
5. Conclusion
Reading a CSV file is possible with many approaches in Java. As Java does not directly have dedicated APIs for CSV handling, we can rely on open-source libraries such as SuperCSV that are very easy to use and highly configurable.
Happy Learning !!
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",ccI want to separate this line such asaa–"a,b"–ccwith using user delimeter . May you give me a suggestion for it.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.
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?
Can we directly replace line1 in file without reading of other lines in CSV file ?
Hi sudha,
Did you complete your requirement.
//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();
}
}
// 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;
}
//———————————————————————————————
}
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
I want to use Apache poi to generate .csv file,as am new to software field,i need some Example code snippets.
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.
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?
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.
How to read the csv file from ftp server, Can you provide sample code.
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)
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.
@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.
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..
Sorry, I don’t have to write from scratch. If you have tried anything, and struck anywhere I can help you.
full java code for creating .csv file by fetching data from database like name,id,email
you can read, wrote, modify and even convert CSV and excel files using Aspose.Cells for Java:
https://products.aspose.com/cells/java/
how can i read a data from .txt file and store it in a string using java.please advise little urgent.
https://howtodoinjava.com/java/io/inputstream-to-string/
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?
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.
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
Hi Sudha, I am sorry. I am not the right guy who can help you in this problem. Try googling.