How to Read a Csv File in Java
How to Read CSV File in Java
The CSV stands for Comma-Separated Values. It is a simple file format which is used to store tabular data in simple text form, such as a spreadsheet or database. The files in the CSV format can be imported to and exported from programs (Microsoft Office and Excel) which store data in tables. The CSV file used a delimiter to identify and separate different data token in a file. The CSV file format is used when we move tabular data between programs that natively operate on incompatible formats. There are following ways to read CSV file in Java. The default separator of a CSV file is a comma (,).
There are following ways to print an array in Java:
- Java Scanner class
- Java String.split() method
- Using OpenCSV API
How to create CSV File
There are two ways to create a CSV file:
- Using Microsoft Excel
- Using Notepad
Using Microsoft Excel
Step 1: Open Microsoft Excel.
Step 2: Write the following data into the file:
Step 3: Now, save the file. Provide the file name CSVDemo and select CSV (Comma delimited) from the save as type menu. Now, click on the Save button.
Using Notepad
Step 1: Open notepad.
Step 2: Write some data into file separated by comma (,). For example:
Vivek, Singh, 23, 9999999, Chandigarh
Step 3: Save the file with .csv extension.
We have created the following file.
Java Scanner class
Java Scanner class provide various methods by which we can read CSV file. The Scanner class provides a constructor that produces values scanned from the specified file. It breaks data into the token form. It uses a delimiter pattern which by default matches white space. The resulting tokens then converted into values of different types using the next() methods.
Example
Output:
Shashank, Mishra, Auditor, 909090090, 45000, Moti Vihar Naveen, Singh, Accountant, 213344455, 12000, Shastri Nagar Mahesh, Nigam, Sr. Manager, 787878878, 30000, Ashok Nagar Manish, Gupta, Manager, 999988765, 20000, Saket Nagar
Java String.split() method
Java String.split() identifies the delimiter and split the rows into tokens.
Syntax
The method parses a delimiting regular expression. The method returns an array of string computed by splitting this string around matches of the given regular expression.
Consider the string:
"this:is:a:table" Regex Result : {"this", "is", "a", "table"}
Example
In the following example, we use BufferedReader class which reads file line by line until the EOF (end of file) character is reached.
Output:
Employee [First Name= Shashank, Last Name= Mishra, Designation= Auditor, Contact= 909090090, Salary= 45000, City= Moti Vihar] Employee [First Name= Naveen, Last Name=Singh, Designation= Accountant, Contact=213344455, Salary= 12000, City= Shastri Nagar] Employee [First Name= Mahesh, Last Name=Nigam, Designation= Sr. Manager, Contact=787878878, Salary= 30000, City= Ashok Nagar] Employee [First Name= Manish, Last Name=Gupta, Designation= Manager, Contact=999988765, Salary= 20000, City= Saket Nagar]
Using OpenCSV API
OpenCSV is a third party API which provide standard libraries to read various versions of CSV file. The library provides better control to handle the CSV file. The library can also read TDF (Tab-Delimited File) file format.
Features of OpenCSV
- Any number of values per line.
- Ignores commas in quoted elements.
- Handles entries that span multiple lines.
The CSVReader class is used to read a CSV file. The class provides CSVReader class constructor to parse a CSV file.
Syntax
Parameters
reader: The reader to a CSV source.
separator: It is a delimiter which is used for separating entries.
Steps to read CSV file in eclipse:
Step 1: Create a class file with the name ReadCSVExample3 and write the following code.
Step 2: Create a lib folder in the project.
Step 3: Download opecsv-3.8.jar from
https://repo1.maven.org/maven2/com/opencsv/opencsv/3.8/opencsv-3.8.jar
Step 4: Copy the opencsv-3.8.jar and paste into the lib folder.
Step 5: Now, run the program.
Example
Output:
Shashank Mishra Auditor 909090090 45000 Moti Vihar Naveen Singh Accountant 213344455 12000 Shastri Nagar Mahesh NigamSr. Manager 787878878 30000 Ashok Nagar Manish Gupta Manager 999988765 20000 Saket Nagar
Reading CSV file with a different separator
In the following CSV file, we have used semicolon (;) to separate tokens.
Example
Output:
Shashank; Mishra; Auditor; 909090090; 45000; Moti Vihar Naveen; Singh; Accountant; 213344455; 12000; Shastri Nagar Mahesh; Nigam; Sr. Manager; 787878878; 30000; Ashok Nagar Manish; Gupta; Manager; 999988765; 20000; Saket Nagar
How to Read a Csv File in Java
Source: https://www.javatpoint.com/how-to-read-csv-file-in-java