Read a CSV file in Java

In this post we will provide an example of how to read a CSV file in Java. This example is using the standard Java libraries, so you will not need to add any external dependencies.

Read a CSV file in Java

We have the following CSV file, it is a list of employee information

The Java code snippet below, does the following:

  • It reads all the lines of the csv file
  • It then removes the first row, which is the header row (assuming we know the format of the csv i.e. ordering of the contents)
  • We then loop through each row and split the string by , so that we get each item in the csv row.
  • You can then process each row as needed, in the case below we have just printed the items

Running the above code produces the following output:

I hope this provides some help in solving your development issue.

Similar posts:

  1. Check list only contains a certain set of values
  2. Spring Boot hello world example
  3. How to use a callable in Java
  4. Java WatchService example

References:

  1. Java String API Documentation
  2. Java Files API Documentation

Leave a Reply