Reading a csv file in java
Posted By : Simran Ahuja | 11-Jan-2021
The CSV means for Comma-Separated Values. It is a simple file format which to stores the tabular data in simple text form, such as a spreadsheet or a database. The files in the CSV format can be imported to as well as exported from programs (Microsoft Office and Excel) which store data in table form. The CSV file uses a delimiter to identify and separate different data tokens in a file. The CSV file format is used where we move tabular data between programs that natively operate on the incompatible types formats of files. There are many ways to read a CSV file in Java. The default separator for a CSV file is a comma (,).
Below are the following ways to access an array in Java:
- Java Scanner class
- Java String.split() method
- Using OpenCSV API
Java Scanner class
Java Scanner class provides various methods by which we can read a CSV file. The Scanner class provides a constructor that produces values scanned from the file specified. It breaks data for us into the token form. It uses a delimiter pattern which by default matches the white space. The tokens generated can be converted into values of different types using the next() methods.
Also Read: Java 8 Function interface
Java String.split() method
Java String.split() identifies the delimiter in the string and split the rows into a number of tokens.
Using OpenCSV API
OpenCSV is a third party API that provides us with the standard libraries to read various versions of a CSV file. The library provides better control to handle us the CSV file. The library can read TDF (Tab-Delimited File) file format also.
Also Read: Cloning of An Object In Java
Features of OpenCSV
- Handles Any number of values per line.
- Ignores commas which are in quoted elements.
- Handles all the entries that span multiple lines.
The CSVReader class is used to read any CSV file. The class provides us with the CSVReader class constructor to parse the CSV file.
- For the maven project, one can include the OpenCSV maven dependency in the file pom.xml in the project which is
<
dependency
>
<
groupId
>com.opencsv</
groupId
>
<
artifactId
>opencsv</
artifactId
>
<
version
>4.1</
version
>
</
dependency
>
- For Gradle Project, you can include the OpenCSV dependency which is.
compile group: 'com.opencsv', name: 'opencsv', version: '4.1'
public
static
void
readData(String file)
{
try
{
// Create an object of filereader
// class with CSV file as a parameter.
FileReader filereader =
new
FileReader(file);
// create csvReader object passing
// file reader as a parameter
CSVReader csvReader =
new
CSVReader(filereader);
String[] nextRecord;
// we are going to read data line by line, each line
while
((nextRecord = csvReader.readNext()) !=
null
) {
for
(String cell : nextRecord) {
System.out.print(cell +
"\t"
);
}
System.out.println();
}
}
catch
(Exception e) {
e.printStackTrace();
}
Choose Oodles For SaaS App Development Services
We are a 360-degree software development company that provides cross-platform SaaS app development services to address varied software project requirements. We have an experienced team of Java, PHP, and Python developers who use advanced frameworks, tools, and SDKs to build scalable web and mobile applications with custom features. For more detail, reach us out at info@oodlestechnologies.com.
About Author
Simran Ahuja
Simran is a bright Java Developer,with good knowledge of core java and advanced java, always ready to face challenges and explore new places.