How to read and write CSV files using core Java

In earlier articles, I explained how to read and writing CSV files using OpenCSV as well as Apache Commons CSV library.

This article is the last one in our short series to learn about 3rd-party libraries and core Java APIs for efficiently reading and writing CSV files.

A CSV file is a plain-text file that stores data in a tabular format, where columns are separated by a delimiter (usually a comma , or a tab). These files are a common choice for importing and exporting data between servers and applications.

In this article, you'll learn how to read and write CSV files using core Java without using any 3rd-party library.

Two important problems that we may face while reading and parsing CSV files:

  1. The field value contains the delimiter. For example, a comma is used as a separator, and the field value containing commas: 1, "Greta, Jones", UK
  2. The field value contains double-quotes, and the double-quotes are used to wrap field values. In such a case, according to RFC 4180, a double-quote that appears inside the field value must be properly-escaped by preceding it with another double-quote: "1", "Greta""Jones", "UK"

If your CSV file contains any of the above things, you should use a 3rd-party library like OpenCSV for reading the CSV file.

Third-party libraries are definitely a better choice for handling different CSV formats, delimiters, and special characters.

However, not all CSV files have such problems. For simple CSV files (without double-quotes and delimiters in field values), core Java is sufficient.

Reading CSV Files in Core Java

In Java, there are multiple ways of reading and parsing CSV files. We will discuss the following two ways of doing so:

  1. Using the Scanner Class
  2. Using BufferedReader and String.split() Method

Here is how our sample CSV file looks:


1,John Deo,,US
2,Alex Jones,,DE
3,Jovan Lee,,FR
4,Greg Hover,,US
4,Emma Watson,,CA

1. Using Scanner Class

The Scanner class in Java breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

try {
    // create scanner instance
    Scanner scanner = new Scanner(Paths.get("users.csv").toFile());

    // set comma as delimiter

    // read all fields
    while (scanner.hasNext()) {
        System.out.print( + " ");

    //close the scanner

} catch (FileNotFoundException ex) {

You'll see the following output:

1 John Deo US
2 Alex Jones DE
3 Jovan Lee FR
4 Greg Hover US
4 Emma Watson CA 

2. Using BufferedReader and String.split() Method

Another way of reading and parsing a CSV file is by using a combination of the BufferedReader class and the String.split() method:

try {
    // CSV file delimiter
    String DELIMITER = ",";

    // create a reader
    BufferedReader br = Files.newBufferedReader(Paths.get("users.csv"));

    // read the file line by line
    String line;
    while ((line = br.readLine()) != null) {
        // convert line into tokens
        String[] tokens = line.split(DELIMITER);

        // TODO: do something here with the data

        // print all tokens
        for (String token : tokens) {

    // close the reader

} catch (IOException ex) {

In the example above, we are doing the following:

  • Open the CSV file for reading by using the Files.newBufferedReader() method.
  • Create an instance of BufferedReader to read the file line by line until the end of file (EOF) is reached.
  • Use the String.split() method to convert each line into multiple tokens by using the comma (,) as a delimiter.
  • The tokens array should contain a list of fields found in each the CSV file row. You should use this array to process the CSV record; like saving it to a database or storing in a Java collection for later use.

Writing CSV Files in Core Java

Writing data to a CSV file is like writing to any other text file in Java. The simplest way is to use the FileWriter class. This is a convenience class for writing streams of characters.

The following example demonstrates how to write a List of objects to a CSV file using the FileWriter in Java:

try {
    // create a list of objects
    List<List<String>> records = Arrays.asList(
            Arrays.asList("1", "John Lee", "US"),
            Arrays.asList("2", "Jovan Roover", "DE"),
            Arrays.asList("3", "Emma Watson", "UK")

    // create a writer
    BufferedWriter writer = Files.newBufferedWriter(Paths.get("users-with-header.csv"));

    // write header record

    // write all records
    for (List<String> record : records) {
        writer.write(String.join(",", record));

    //close the writer

} catch (IOException ex) {

Here is how the newly created CSV file looks like:

1,John Lee,US
2,Jovan Roover,DE
3,Emma Watson,UK


In this tutorial, we learned how to read and write CSV files using core Java without any 3rd-party library. You can use either the Scanner class or BufferedReader to read and parse a CSV file line by line. For writing to CSV files, you should use the FileWriter class.

This solution is intended for reading and writing simple CSV files. For complex CSV files with multiple delimiters, double-quotes, and special characters, you should use 3rd-party libraries.

Further Reading

If you enjoy reading this article, you may also be interested in reading other CSV related articles:

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.