Read Multiple Rows From Csv File Java Scanner
Nosotros tin can use Java Scanner Class to read CSV File in java.
Read CSV File in Coffee
We can employ Coffee Scanner class to read CSV file and convert to collection of coffee edible bean. For instance, we might have a CSV file like below.
employees.csv
1,Pankaj Kumar,Developer,5000 USD 2,Mani,Developer,4000 USD 3,Avinash,Developer,5000 USD 4,David,QA Atomic number 82,4000 USD And we accept a java bean that maps to different columns in the CSV file.
Employee.java
package com.journaldev.csv; public class Employee { individual int id; private String proper noun; private Cord function; private String bacon; 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.part = function; } public Cord getSalary() { return salary; } public void setSalary(Cord salary) { this.salary = bacon; } @Override public String toString(){ return "\nID="+getId()+"::Name"+getName()+"::Role="+getRole()+"::Salary="+getSalary(); } } Let'south 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 Cavalcade – Employee Salary
Now we can utilise Scanner class to parse CSV file and create collection of Employees.
ReadCSVWithScanner.java
bundle com.journaldev.csv; import java.io.BufferedReader; import java.io.FileReader; import coffee.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 = zilch; Scanner scanner = nada; int alphabetize = 0; List<Employee> empList = new ArrayList<>(); while ((line = reader.readLine()) != goose egg) { 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 (alphabetize == 2) emp.setRole(data); else if (index == three) emp.setSalary(information); else System.out.println("invalid information::" + data); index++; } index = 0; empList.add(emp); } //close reader reader.close(); Organization.out.println(empList); } } Notice that we are setting scanner delimiter equally comma (,). If input file uses some other delimiter such as pipe (|) or hash (#), and so all we demand to practise is alter the delimiter pattern in above program.
In one case we run above program, it prints post-obit output.
[ ID=1::NamePankaj Kumar::Role=Developer::Salary=5000 USD, ID=2::NameMani::Role=Programmer::Salary=4000 USD, ID=3::NameAvinash::Role=Programmer::Salary=5000 USD, ID=4::NameDavid::Role=QA Lead::Bacon=4000 USD] If you look into Scanner grade constructor, you lot will notice that it accepts File or InputStream every bit input. As well information technology contains utility method hasNextLine() and nextLine() that nosotros can use to parse CSV file using Scanner only.
CSVScannerExample.coffee
packet com.journaldev.csv; import java.io.File; import java.io.IOException; import java.util.ArrayList; import coffee.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; Listing<Employee> empList = new ArrayList<>(); while (scanner.hasNextLine()) { dataScanner = new Scanner(scanner.nextLine()); dataScanner.useDelimiter(","); Employee emp = new Employee(); while (dataScanner.hasNext()) { Cord information = 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 information::" + information); index++; } alphabetize = 0; empList.add(emp); } scanner.shut(); Arrangement.out.println(empList); } } If you run in a higher place program, the output produced will be same as above program. Scanner grade is a good choice if all you demand is to parse a simple CSV file.
Source: https://www.journaldev.com/2335/read-csv-file-java-scanner
0 Response to "Read Multiple Rows From Csv File Java Scanner"
Post a Comment