How to read CSV file in Java

CSV files are used to save tabular data in plain text format. A delimiter is used to identify and separate different data tokens in the file. There are multiple ways to read CSV files. In this article, we look at how to read a CSV file in Java.

Java Program to Read CSV File

Method 1: Using Scanner

The Scanner class is used to get the user input and it is present java.util.* package. A Scanner splits its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens can be converted to values ​​of various types using the following methods.

For reading the CSV file, we will use a delimiter. We can set the delimiter using the useDelimiter() function. Below is the Java program:

import java.io.*;
import java.util.*;

public class readCSVFile {

  public static void main(String[] args) throws FileNotFoundException{
    // Read the file path
    Scanner sc = new Scanner(new File("C:\\Users\\Abinash\\Downloads\\a1-burtin.csv"));
    // Set the delimiter used in file.
    sc.useDelimiter(",");
    
    while(sc.hasNext()) {
      System.out.print(sc.next()+ "|");
    }
    sc.close();
  }

}

Output

Bacteria |Penicilin|Streptomycin |Neomycin|Gram Staining 
Aerobacter aerogenes|870|1|1.6|negative
Brucella abortus|1|2|0.02|negative
Brucella anthracis|0.001|0.01|0.007|positive
Diplococcus pneumoniae|0.005|11|10|positive
Escherichia coli|100|0.4|0.1|negative
Klebsiella pneumoniae|850|1.2|1|negative
Mycobacterium tuberculosis|800|5|2|negative
Proteus vulgaris|3|0.1|0.1|negative

Method 2: Using split() function

The String.split() function creates a token from the specified string into tokens based on the delimiter specified as the parameter.

Use the BufferedReader class to read line by line from the input CSV file. Then use a delimiter to split each line into tokens.

package PraticeProblems;

import java.io.*;
public class readCSVUsingSplit {

  public static void main(String[] args) {
// input file path String filePath = "C:\\Users\\Abinash\\Downloads\\a1-burtin.csv"; BufferedReader fileReader = null; try { String line = ""; fileReader = new BufferedReader(new FileReader(filePath));
// Read line-by-line while((line = fileReader.readLine()) != null) {
//split each line using "," String[] data_tokens = line.split(",");
// display data token for(String data: data_tokens) { System.out.print(data+ "|"); } System.out.println(); } } catch(Exception e) { System.out.println(e); } finally { try {
// close the fileReader fileReader.close(); }catch(IOException e) { System.out.println(e); } } } }

Output

Bacteria |Penicilin|Streptomycin |Neomycin|Gram Staining |
Aerobacter aerogenes|870|1|1.6|negative|
Brucella abortus|1|2|0.02|negative|
Brucella anthracis|0.001|0.01|0.007|positive|
Diplococcus pneumoniae|0.005|11|10|positive|
Escherichia coli|100|0.4|0.1|negative|
Klebsiella pneumoniae|850|1.2|1|negative|
Mycobacterium tuberculosis|800|5|2|negative|
Proteus vulgaris|3|0.1|0.1|negative|

Also, refer

Java Program to merge two files in a third file

Count the Number of Lines in a Text File in Java

Leave a Reply

Your email address will not be published. Required fields are marked *