All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Read A CSV File In Java With Example

Last Updated : Aug 7, 2023

How To Read A CSV File In Java With Example

In this article we will show you the solution of how to read a csv file in java with example, a CSV file is a file with commas separated by values. This simple file format allows you to store tabular data in simple text form, like in a spreadsheet or database.

It is possible to import and export data in the CSV format via programs (Microsoft Office and Excel) that store the data in tables.

To distinguish and separate different types of data tokens in CSV files, there were delimiters.

In situations where tabular data must be moved between programs that cannot communicate natively, the CSV file format is used.

The following are the methods that Java provides for reading CSV files. Commas (,) are used as the separator in CSV files. We will now discuss the idea of how to read a csv file in java with an example.

Step By Step Guide On How To Read A CSV File In Java With Example :-

import java.io.FileReader;
import java.io.IOException;
import com.opencsv.CSVReader;
public class TalkersCodeCsv
{
public static void main(String[] args)
{
CSVReader reader = null;
try
{
reader = new CSVReader(new FileReader("F:\\test.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null)
{
for(String token : nextLine)
{
System.out.println(token);
}
System.out.print("\n");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
  1. As a first step, the OpenCSV library is used to import the classes required for reading a CSV file.
  2. As the class name implies, TalkersCodeCsv is defined.
  3. Upon creating a class, it is necessary to define a main method.
  4. Null is declared and initialized as the first parameter of the CSVReader object.
  5. There is a try-catch block started to handle any exceptions that might occur.
  6. Upon initialization, the constructor of the CSVReader class is called with the path to the CSV file as an argument. The specified file will be opened by the CSVReader object created by this method.
  7. Using the CSV file as the source of information, each line is stored in a String array.
  8. There is no end to the while loop as long as there is information to read from the file.
  9. Strings are created by reading the next line of data from the file and storing it in the readNext() method.
  10. String array elements are iterated through using a for-each loop.
  11. Printing each element of the array to the console is achieved by calling the println() method.
  12. In order to separate each line of data from the CSV file, an empty line is printed to the console.
  13. Any exception thrown during the try block is caught in the catch block.
  14. When an exception occurs, the printStackTrace() method is used to output a stack trace to the console. This can be used to help debug the problem.

Conclusion :-

As a result, we have successfully learned how to read a csv file in java with an example.

The purpose of this Java tutorial was to teach you different methods for reading CSV files in Java.

The Java CSV file can be read using any one of the three methods described above.

We can read CSV files using the Java OpenCSV API, which is built into Eclipse. Using Java's CSV libraries is one of the simplest ways to read and write CSV files.

I hope this article on how to read a csv file in java with example helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪