Parse CSV files in java
A comma-separated values(CSV) file is a normal plain-text file that uses a comma to separate values. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. like
We can use pipe| or colon: etc. as a separator as well
There are many ways to read CSV files using java.
Here we discuss the following ways to read CSV files using java
Table of Content :
- Introduction
- Use BufferedReader class from the java.io package
- Read a CSV file line by line and convert each line into an object representing that data using the Scanner class
- Use a third-party library like Apache Commons CSV
- Read a CSV file in Java using OpenCSV
- Questions related to Parse CSV files in Java
- Summary
1). Use BufferedReader class from the java.io package.
2). Read a CSV file line by line and convert each line into an object representing that data using the Scanner class.
3). Use a third-party library like Apache Commons CSV.
4). Read a CSV file in Java using OpenCSV.
1). Use BufferedReader class from the java.io package.
Here, we have used BufferedReader to read the CSV file, line by line, split line string using a delimiter, and create an object from a String array.
orders.csv
1,1033,2000.00 ,"confirmed"
2,1012,4500.00 ,"pending"
3,1054,500.00 ,"shipped"
4,1012,5000.00 ,"delivered"
Order POJO, We have to bind order data to this POJO.
package com.javacodestuffs.csv.examples;
public class Order {
private long id;
private int customerId;
private double amount;
private String status;
public Order(long id, int customerId, double amount, String status) {
this.id = id;
this.customerId = customerId;
this.amount = amount;
this.status = status;
}
//@Getter @Setter @toString()
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public String toString() {
return "Order [id=" + id + ", customerId=" + customerId + ", amount=" + amount + ", status=" + status + "]";
}
}
package com.javacodestuffs.csv.examples;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ReadCSVUsingBufferedReader {
public static void main(String args[]) {
String filePath = "src/main/resources/orders.csv";
System.out.println("--------------Order data is---------------------");
List<Order> orders = readOrdersFromCSV(filePath);
for (Order order : orders) {
System.out.println(order);
}
System.out.println("----------------------------------------------");
}
private static List<Order>readOrdersFromCSV(String fileName) {
List<Order> orders = new ArrayList<Order>();
try {
FileReader fileReader = new FileReader(fileName);
// create BufferedReader object
BufferedReader br = new BufferedReader(fileReader);
String line;
while ((line = br.readLine()) != null) {
String[] order = line.split(",");// separate line by comma or we can use any valid delimeter in csv file
orders.add(getOrder(order));
}
} catch (IOException ex) {
System.out.println("Exception occured during reading the csv file" + ex);
}
return orders;
}
private static Order getOrder(String[] order) {
Long id = Long.parseLong(order[0]);
int customerId = Integer.parseInt(order[1]);
Double amount = Double.parseDouble(order[2]);
String status = order[3].toString();
return new Order(id, customerId, amount, status);
}
}
Separate the order fields from an array
private static Order getOrder(String[] order) {
Long id = Long.parseLong(order[0]);
int customerId = Integer.parseInt(order[1]);
Double amount = Double.parseDouble(order[2]);
String status = order[3].toString();
return new Order(id, customerId, amount, status);
}
Output:
--------------Order data is---------------------
Order [id=1, customerId=1033, amount=2000.0, status="confirmed"]
Order [id=2, customerId=1012, amount=4500.0, status="pending"]
Order [id=3, customerId=1054, amount=500.0, status="shipped"]
Order [id=4, customerId=1012, amount=5000.0, status="delivered"]
----------------------------------------------
2). Read a CSV file line by line and convert each line into an object
representing that data using the Scanner class.
we're going to use a java.util.Scanner to run through the contents of the file and retrieve lines serially, one by one.
String filePath = "src/main/resources/orders.csv";
List<List<String>> orderList = new ArrayList<>();
try (Scanner scanner = new Scanner(new File(filePath))) {
while (scanner.hasNextLine()) {
orderList.add(getOrderFromLine(scanner.nextLine()));
}
}
Then we will parse the lines and store them in an array.
private static List<String>getOrderFromLine(String orderLine) {
List<String>values = new ArrayList<String>();
try (Scanner scan = new Scanner(orderLine)) {
scan.useDelimiter(",");
while (scan.hasNext()) {
values.add(scan.next());
}
}
return values;
}
ReaderCSVUsingScanner.java
package com.javacodestuffs.csv.examples;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ReadCSVUsingScanner {
public static void main(String args[]) throws FileNotFoundException {
String filePath = "src/main/resources/orders.csv";
List<List<String>> orderList = new ArrayList<>();
try (Scanner scanner = new Scanner(new File(filePath))) {
while (scanner.hasNextLine()) {
orderList.add(getOrderFromLine(scanner.nextLine()));
}
}
System.out.println("--------------Order data is---------------------");
for (List<String> order : orderList) {
System.out.println(order);
}
System.out.println("----------------------------------------------");
}
private static List<String>getOrderFromLine(String orderLine) {
List<String>values = new ArrayList<String>();
try (Scanner scan = new Scanner(orderLine)) {
scan.useDelimiter(",");
while (scan.hasNext()) {
values.add(scan.next());
}
}
return values;
}
}
output:
--------------Order CSV data using ReaderCSVUsingScanner ---------------------
[1, 1033, 2000.00 , "confirmed"]
[2, 1012, 4500.00 , "pending"]
[3, 1054, 500.00 , "shipped"]
[4, 1012, 5000.00 , "delivered"]
----------------------------------------------
3). Use a third-party library like Apache Commons CSV.
The Apache Commons library offers several methods to access individual fields in a CSV file. Commons CSV has built-in support to read the most common CSV formats e.g. RFC 4180, Microsoft Excel, MySQL, and TDF.
If your CSV file does not contain a header, or with the header, it can handle it by code.
CSVParser can parse different kinds of CSV files e.g. XLS CSV file, CSV file without a header, or CSV file with a header.
Maven dependency
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.8</version>
</dependency>
public static void main(String args[]) throws IOException {
String filePath = "src/main/resources/orders.csv";
String filePath1 = "src/main/resources/orders2.csv";
System.out.println("Reading from CSV file using BufferedReader and String Split");
List<Order> orders = readCSV(filePath);
printOrder(orders);
parseOrderCSVWithHeaders(filePath1);
System.out.println("---------------Parsing CSV file-------------------------");
parseOrderCSV(filePath);
}
private static List<Order> readCSV(String filePath) throws FileNotFoundException, IOException {
List<Order> orders = new ArrayList<Order>();
BufferedReader br = new BufferedReader(new FileReader(filePath));
String line = br.readLine();
// Reading header, Ignoring
while ((line = br.readLine()) != null && !line.isEmpty()) {
String[] orderLine = line.split(",");
Long id = Long.parseLong(orderLine[0]);
Integer customerId = Integer.parseInt(orderLine[1]);
Double amount = Double.parseDouble(orderLine[2]);
String status = orderLine[3].toString();
orders.add(new Order(id, customerId, amount, status));
}
return orders;
}
return orders; }
If our CSV file contains the header
id, customerId, amount ,status
then we have to use like this
private static void parseOrderCSV(String filePath) throws FileNotFoundException, IOException {
String[] headers = {"id","customerId","amount","status"};
CSVParser parser = new CSVParser(new FileReader(filePath), CSVFormat.DEFAULT.withHeader(headers));
for (CSVRecord order : parser) {
System.out.printf("%s\t%s\t%s\t%s\n", order.get("id"), order.get("customerId"), order.get("amount"),
order.get("status"));
}
parser.close();
}
public static void printOrder(List orders) {
System.out.println("--------------Order data is---------------------");
for (Order order: orders) {
System.out.println(order);
}
System.out.println("----------------------------------------------");
}
--------------Order data is---------------------
Order [id=2, customerId=1012, amount=4500.0, status="pending"]
Order [id=3, customerId=1054, amount=500.0, status="shipped"]
Order [id=4, customerId=1012, amount=5000.0, status="delivered"]
package com.javacodestuffs.csv.examples;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
public class ReadCSVUsingApacheCommonCSV {
public static void main(String args[]) throws IOException {
String filePath = "src/main/resources/orders.csv";
String filePath1 = "src/main/resources/orders2.csv";
System.out.println("Reading from CSV file using BufferedReader and String Split");
List<Order> orders = readCSV(filePath);
printOrder(orders);
parseOrderCSVWithHeaders(filePath1);
System.out.println("---------------Parsing CSV file-------------------------");
parseOrderCSV(filePath);
}
private static void parseOrderCSV(String filePath) throws FileNotFoundException, IOException {
String[] headers = {"id","customerId","amount","status"};
CSVParser parser = new CSVParser(new FileReader(filePath), CSVFormat.DEFAULT.withHeader(headers));
for (CSVRecord order : parser) {
System.out.printf("%s\t%s\t%s\t%s\n", order.get("id"), order.get("customerId"), order.get("amount"),
order.get("status"));
}
parser.close();
}
private static List<Order> readCSV(String filePath) throws FileNotFoundException, IOException {
List<Order> orders = new ArrayList<Order>();
BufferedReader br = new BufferedReader(new FileReader(filePath));
String line = br.readLine();
// Reading header, Ignoring
while ((line = br.readLine()) != null && !line.isEmpty()) {
String[] orderLine = line.split(",");
Long id = Long.parseLong(orderLine[0]);
Integer customerId = Integer.parseInt(orderLine[1]);
Double amount = Double.parseDouble(orderLine[2]);
String status = orderLine[3].toString();
orders.add(new Order(id, customerId, amount, status));
}
return orders;
}
public static void parseOrderCSVWithHeaders(String filePath) throws FileNotFoundException, IOException {
CSVParser parser = new CSVParser(new FileReader(filePath), CSVFormat.DEFAULT.withHeader());
List<Order> records = parser.getRecords();
for (CSVRecord order : records) {
System.out.printf("%s\t%s\t%s\n", order.get("id"), order.get("customerId"), order.get("amount"),
order.get("status"));
}
parser.close();
}
public static void printOrder(List<Order> orders) {
System.out.println("--------------Order data is---------------------");
for (Order order : orders) {
System.out.println(order);
}
System.out.println("----------------------------------------------");
}
}
4). Read a CSV file in Java using OpenCSV.
It is a java library commonly used for parsing CSV files. It provides more functionality to map the column data to the java bean and much more.
To use OpenCSV we need a minimum java version of Java7.
We need Maven dependency as
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.2</version>
</dependency>
Suppose we have a CSV file as order.csv
id, customerId, amount ,status
1, 1001,2000.00 ,"confirmed"
2, 1001,4500.00 ,"pending"
3, 1001,500.00 ,"shipped"
4, 1001,5000.00 ,"dilivered"
1). Read CSV by line by line
package com.javacodestuffs.csv.examples;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ReadCSVUsingBufferedReader {
public static void main(String args[]) {
String filePath = "src/main/resources/orders.csv";
System.out.println("--------------Order data is---------------------");
Listt<Order> orders = readOrdersFromCSV(filePath);
for (Order order : orders) {
System.out.println(order);
}
System.out.println("----------------------------------------------");
}
private static Listt<Order> readOrdersFromCSV(String fileName) {
Listt<Order> orders = new ArrayListt<Order>();
try {
FileReader fileReader = new FileReader(fileName);
// create BufferedReader object
BufferedReader br = new BufferedReader(fileReader);
String line;
while ((line = br.readLine()) != null) {
String[] order = line.split(",");// separate line by comma or we can use any valid delimeter in csv file
orders.add(getOrder(order));
}
} catch (IOException ex) {
System.out.println("Exception occured during reading the csv file" + ex);
}
return orders;
}
private static Order getOrder(String[] order) {
Long id = Long.parseLong(order[0]);
int customerId = Integer.parseInt(order[1]);
Double amount = Double.parseDouble(order[2]);
String status = order[3].toString();
return new Order(id, customerId, amount, status);
}
}
output:
--------------Order data is---------------------
Order [id=1, customerId=1033, amount=2000.0, status="confirmed"]
Order [id=2, customerId=1012, amount=4500.0, status="pending"]
Order [id=3, customerId=1054, amount=500.0, status="shipped"]
Order [id=4, customerId=1012, amount=5000.0, status="delivered"]
----------------------------------------------
2). Read data in one shot
package com.javacodestuffs.csv.examples;
import java.io.FileReader;
import java.util.List;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
public class ReadAllUsingCSVReader {
public static void main(String args[]) {
String filePath = "src/main/resources/orders2.csv";
try {
FileReader filereader = new FileReader(filePath);
// create csvReader object and skip first Line
CSVReader csvReader = new CSVReaderBuilder(filereader).withSkipLines(1).build();
List<String[]> orderData = csvReader.readAll();
System.out.println("--------------Order data is---------------------");
for (String[] orders : orderData) {
for (String orderString : orders) {
System.out.print(orderString + "\t");
}
System.out.println();
}
System.out.println("----------------------------------------------");
} catch (Exception ex) {
System.out.println("Exception occured during reading the csv file" + ex);
}
}
}
output:
--------------Order data is---------------------
1 1033 2000.00 confirmed
2 1012 4500.00 pending
3 1054 500.00 shipped
4 1012 5000.00 delivered
----------------------------------------------
Questions related to Parse CSV files in Java
Read file to byte[] array in Java.How to read Files from the Classpath in the Spring.
Java File Reader Examples.
How to Read the file from the resources folder in Java.
Files in Java.
In this article, we have seen How to Parse CSV files in Java using different ways. All source code in the article can be found in the GitHub repository. You can also download the code from Here.
0 Comments
Post a Comment