We can use Java Scanner Class to read CSV File in java.
Read CSV File in Java
We can use Java Scanner class to read CSV file and convert to collection of java bean. For example, we might have a CSV file like below.
employees.csv
1,Pankaj Kumar,Developer,5000 USD
2,Mani,Programmer,4000 USD
3,Avinash,Developer,5000 USD
4,David,QA Lead,4000 USD
And we have a java bean that maps to different columns in the CSV file.
Employee.java
package com.journaldev.csv;
public class Employee {
private int id;
private String name;
private String role;
private String salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
@Override
public String toString(){
return "nID="+getId()+"::Name"+getName()+"::Role="+getRole()+"::Salary="+getSalary();
}
}
Let’s say that employee bean variables maps to following columns in CSV file.
1st Column – Employee ID
2nd Column – Employee Name
3rd Column – Employee Role
4th Column – Employee Salary
Now we can use Scanner class to parse CSV file and create collection of Employees.
ReadCSVWithScanner.java
package com.journaldev.csv;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ReadCSVWithScanner {
public static void main(String[] args) throws IOException {
// open file input stream
BufferedReader reader = new BufferedReader(new FileReader(
"employees.csv"));
// read file line by line
String line = null;
Scanner scanner = null;
int index = 0;
List<Employee> empList = new ArrayList<>();
while ((line = reader.readLine()) != null) {
Employee emp = new Employee();
scanner = new Scanner(line);
scanner.useDelimiter(",");
while (scanner.hasNext()) {
String data = scanner.next();
if (index == 0)
emp.setId(Integer.parseInt(data));
else if (index == 1)
emp.setName(data);
else if (index == 2)
emp.setRole(data);
else if (index == 3)
emp.setSalary(data);
else
System.out.println("invalid data::" + data);
index++;
}
index = 0;
empList.add(emp);
}
//close reader
reader.close();
System.out.println(empList);
}
}
Notice that we are setting scanner delimiter as comma (,). If input file uses some other delimiter such as pipe (|) or hash (#), then all we need to do is change the delimiter pattern in above program.
Once we run above program, it prints following output.
[
ID=1::NamePankaj Kumar::Role=Developer::Salary=5000 USD,
ID=2::NameMani::Role=Programmer::Salary=4000 USD,
ID=3::NameAvinash::Role=Developer::Salary=5000 USD,
ID=4::NameDavid::Role=QA Lead::Salary=4000 USD]
If you look into Scanner class constructor, you will notice that it accepts File or InputStream as input. Also it contains utility method hasNextLine()
and nextLine()
that we can use to parse CSV file using Scanner only.
CSVScannerExample.java
package com.journaldev.csv;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CSVScannerExample {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(new File("employees.csv"));
Scanner dataScanner = null;
int index = 0;
List<Employee> empList = new ArrayList<>();
while (scanner.hasNextLine()) {
dataScanner = new Scanner(scanner.nextLine());
dataScanner.useDelimiter(",");
Employee emp = new Employee();
while (dataScanner.hasNext()) {
String data = dataScanner.next();
if (index == 0)
emp.setId(Integer.parseInt(data));
else if (index == 1)
emp.setName(data);
else if (index == 2)
emp.setRole(data);
else if (index == 3)
emp.setSalary(data);
else
System.out.println("invalid data::" + data);
index++;
}
index = 0;
empList.add(emp);
}
scanner.close();
System.out.println(empList);
}
}
If you run above program, the output produced will be same as above program. Scanner class is a good choice if all you need is to parse a simple CSV file.