Sunday, January 14, 2024

How to load data from CSV file in Java - Example

You can load data from a CSV file in a Java program by using BufferedReader class from the java.io package. You can read the file line by line and convert each line into an object representing that data. Actually, there are a couple of ways to read or parse CSV files in Java e.g. you can use a third-party library like Apache commons CSV or you can use Scanner class, but in this example, we will use the traditional way of loading CSV files using BufferedReader.


Here are the steps to load data from CSV file in Java without using any third-party library :
  • Open CSV file using FileReader object
  • Create BufferedReader from FileReader
  • Read file line by line using readLine() method
  • Split each line on comma to get an array of attributes using String.split() method
  • Create an object of Book class from String array using new Book()
  • Add those object into ArrayList using add() method
  • Return the List of books to the caller

And here is our sample CSV file which contains details of my favorite books. It's called books.csv, each row represents a book with the title, price, and author information. The first column is the title of the book, the second column is the price, and the third column is the author of the book.
Effective Java,42,Joshua Bloch
Head First Java,39,Kathy Sierra
Head First Design Pattern,44,Kathy Sierra
Introduction to Algorithm,72,Thomas Cormen




Step by Step guide to load a CSV file in Java

Let's go through each step to find out what they are doing and how they are doing :

1. Reading the File

To read the CSV file we are going to use a BufferedReader in combination with a FileReader. FileReader is used to read a text file in the platform's default character encoding, if your file is encoded in other character encodings then you should use InputStreamReader instead of FileReader class. 

We will read one line at a time from the file using readLine() method until the EOF (end of file) is reached, in that case, readLine() will return a null.


2. Split comma separated String

We take the string that we read from the CSV file and split it up using the comma as the 'delimiter' (because it's a CSV file). This creates an array with all the columns of the CSV file as we want, however, values are still in Strings, so we need to convert them into proper type e.g. prices into float type as discussed in my post how to convert String to float in Java

Once we got all the attribute values, we create an object of the Book class by invoking the constructor of the book as a new Book() and pass all those attributes. Once we Book objects then we simply add them to our ArrayList.

How to load CSV file in Java using BufferedReader


Java Program to load data from CSV file

Here is our full program to read a CSV file in Java using BufferedReader. It's a good example of how to read data from a file line by line, split string using a delimiter, and how to create objects from a String array in Java. 

Once you load data into the program you can insert it into a database, or you can persist into some other format or you can send it over the network to another JVM.

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * Simple Java program to read CSV file in Java. In this program we will read
 * list of books stored in CSV file as comma separated values.
 * 
 * @author WINDOWS 8
 *
 */
public class CSVReaderInJava {

    public static void main(String... args) {
        List<Book> books = readBooksFromCSV("books.txt");

        // let's print all the person read from CSV file
        for (Book b : books) {
            System.out.println(b);
        }
    }

    private static List<Book> readBooksFromCSV(String fileName) {
        List<Book> books = new ArrayList<>();
        Path pathToFile = Paths.get(fileName);

        // create an instance of BufferedReader
        // using try with resource, Java 7 feature to close resources
        try (BufferedReader br = Files.newBufferedReader(pathToFile,
                StandardCharsets.US_ASCII)) {

            // read the first line from the text file
            String line = br.readLine();

            // loop until all lines are read
            while (line != null) {

                // use string.split to load a string array with the values from
                // each line of
                // the file, using a comma as the delimiter
                String[] attributes = line.split(",");

                Book book = createBook(attributes);

                // adding book into ArrayList
                books.add(book);

                // read next line before looping
                // if end of file reached, line would be null
                line = br.readLine();
            }

        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        return books;
    }

    private static Book createBook(String[] metadata) {
        String name = metadata[0];
        int price = Integer.parseInt(metadata[1]);
        String author = metadata[2];

        // create and return book of this metadata
        return new Book(name, price, author);
    }

}

class Book {
    private String name;
    private int price;
    private String author;

    public Book(String name, int price, String author) {
        this.name = name;
        this.price = price;
        this.author = author;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book [name=" + name + ", price=" + price + ", author=" + author
                + "]";
    }

}

Output
Book [name=Effective Java, price=42, author=Joshua Bloch]
Book [name=Head First Java, price=39, author=Kathy Sierra]
Book [name=Head First Design Pattern, price=44, author=Kathy Sierra]
Book [name=Introduction to Algorithm, price=72, author=Thomas Cormen]


That's all about how to load CSV files in Java without using any third-party library.  You have learned how to use BufferedReader to read data from CSV files and then how to split comma-separated String into String array by using the String.split() method. 


If you like this tutorial and interested to learn more about how to deal with files and directories in Java, you can check out the following Java IO tutorial :
  • How to read Excel Files in Java using Apache POI? [example]
  • How to append data into an existing file in Java? [example]
  • 2 ways to read a file in Java? [tutorial]
  • How to create a file or directory in Java? [example]
  • How to read a text file using the Scanner class in Java? [answer]
  • How to write content into a file using BufferedWriter class in Java? [example]
  • How to read username and password from the command line in Java? [example]
  • How to read Date and String from Excel file in Java? [tutorial]
Though you read CSV files in Java even more easily by using a third-party library like Apache commons CSV, knowing how to do it using pure Java will help you to learn key classes from JDK. 

29 comments:

  1. Looks poorly done. What about book names containing a comma? You'll have to manage string delimiters. But then what about strings containing the chosen delimiter? You'll have to escape and unescape it, right?
    This is why you don't reinvent cold water and just use a library from the start, instead of rewrite your program later because you didn't take these aspects into account.

    ReplyDelete
  2. This is right up to the point for me, thanks for sharing.

    ReplyDelete
  3. what is the use of getter and setter methods in above example ? Are they even used in the above example ?

    ReplyDelete
  4. Very poorly done. This article should be renamed to say 'comma delimited non-quoted, alphanumeric text file reader' ... probably prefixed with the word 'unsafe'. My advice: just remove the article - as your profile states you are a "Software Architect" this article does not help your CV.

    ReplyDelete
  5. Even when Mister Anonymous says your Article is poorly done, it helped me very much to develope one my first programming projects!
    Keep Going mate! :)

    ReplyDelete
    Replies
    1. I was looking to read a csv file, like Books.csv
      not Books.text
      Am I missing anything ?

      Delete
  6. What about book names containing a comma? :((((((((((((((((((((((((((((((((((((((

    ReplyDelete
  7. What about book names containing a comma????????????????????????????????????????????????

    ReplyDelete
    Replies
    1. That's tricky because comma is used as a separate, check Jackson CSV library if it provides some options.

      Delete
    2. if you don't know what is Jackson CSV and how it helps you to parse CSV file with header check that tutorial.

      Delete
  8. Helped me to some extent but my data is repetitive and I have to group it by use of instance

    ReplyDelete
    Replies
    1. Once you load in memory, you can do group by using Java 8 Stream API. You may find my earlier post on 10 Stream Examples in Java useful where I have talked about partition by and group by kind of stuff.

      Delete
  9. Where is the location of file??

    ReplyDelete
  10. what if i want to print "Effective Java" only from that class..Pls help:)

    ReplyDelete
  11. what about quotes and double quotes!??!

    ReplyDelete
    Replies
    1. use Jackson CSV library, its much more feature rich and can handle different types of CSV files with header and without header. I have also written a tutorial about how to parse CSV file with header using Jackson, you may want to check that.

      Delete
  12. How do i skip the first row i.e; the header line using this program? also how do i get only selected columns into my class? for example if i only need to get books and price, without making any changes to my csv file?

    Also i need to know how can i manipulate a specific column according to another column? like suppose i need to increase one of the books price by 1?

    ReplyDelete
    Replies
    1. Hello @Unknown, for you that you need to code it yourself, for example create a boolean variable to indicate if header is present or not and if its true start reading from second line. though, I suggest you better use a CSV library like Jackson, which does all this for you. Here is an example of reading CSV file with header using Jackson, you will find most of your answer there. All the best

      Delete
  13. Thanks for a nice clear example.

    ReplyDelete
  14. for some reason my csv file does not open, any help?

    ReplyDelete
  15. HI Team i have a requirement like to read the csv file and the particular column get the max value like there are comma separated values like id,name,age,room and those are separated by new line delimeter id,name,age,room \n1,raj,23,3/n2,shiva,34,8 in the file i need to take two arguments in method one is this file and second one in C column like age in that age need to fetch the max value by using java 8

    ReplyDelete
  16. Very great example, it helped me, but where does the toString method would be placed?

    ReplyDelete
  17. Thankyou so much for this fantastic tutorial/instructional! really helped me (im just learning JAVA) :)

    ReplyDelete
    Replies
    1. Glad that it helped, all the best with your Java learning journey.

      Delete

Feel free to comment, ask questions if you have any doubt.