How to Read a Csv File Into Hashmap 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 every bit a spreadsheet or database. The files in the CSV format can be imported to and exported from programs (Microsoft Role and Excel) which store information in tables. The CSV file used a delimiter to place and divide different data token in a file. The CSV file format is used when nosotros move tabular data between programs that natively operate on incompatible formats. There are post-obit ways to read CSV file in Coffee. The default separator of a CSV file is a comma (,).
At that place are following ways to print an array in Java:
- Coffee Scanner form
- Java Cord.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 information into the file:
Step 3: Now, salvage the file. Provide the file name CSVDemo and select CSV (Comma delimited) from the save every bit type card. Now, click on the Salve button.
Using Notepad
Pace one: Open up notepad.
Step two: Write some data into file separated by comma (,). For example:
Vivek, Singh, 23, 9999999, Chandigarh
Step 3: Salve the file with .csv extension.
We have created the post-obit file.
Java Scanner class
Java Scanner course 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 course. Information technology uses a delimiter pattern which past default matches white space. The resulting tokens then converted into values of different types using the next() methods.
Case
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
Coffee Cord.split() method
Java String.split() identifies the delimiter and separate the rows into tokens.
Syntax
The method parses a delimiting regular expression. The method returns an array of string computed past splitting this string around matches of the given regular expression.
Consider the string:
"this:is:a:tabular array" Regex Outcome : {"this", "is", "a", "table"}
Example
In the post-obit example, nosotros use BufferedReader class which reads file line by line until the EOF (cease of file) character is reached.
Output:
Employee [Start Proper name= Shashank, Terminal Name= Mishra, Designation= Auditor, Contact= 909090090, Salary= 45000, City= Moti Vihar] Employee [Starting time Name= Naveen, Last Name=Singh, Designation= Accountant, Contact=213344455, Salary= 12000, City= Shastri Nagar] Employee [Offset Proper name= Mahesh, Last Name=Nigam, Designation= Sr. Manager, Contact=787878878, Salary= 30000, City= Ashok Nagar] Employee [Beginning Name= Manish, Last Name=Gupta, Designation= Director, Contact=999988765, Bacon= 20000, City= Saket Nagar]
Using OpenCSV API
OpenCSV is a tertiary political 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 form constructor to parse a CSV file.
Syntax
Parameters
reader: The reader to a CSV source.
separator: Information technology 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 post-obit code.
Step 2: Create a lib binder in the project.
Step 3: Download opecsv-iii.8.jar from
https://repo1.maven.org/maven2/com/opencsv/opencsv/3.8/opencsv-3.8.jar
Stride 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 Auditor 213344455 12000 Shastri Nagar Mahesh NigamSr. Manager 787878878 30000 Ashok Nagar Manish Gupta Manager 999988765 20000 Saket Nagar
Reading CSV file with a unlike separator
In the following CSV file, we have used semicolon (;) to separate tokens.
Example
Output:
Shashank; Mishra; Accountant; 909090090; 45000; Moti Vihar Naveen; Singh; Accountant; 213344455; 12000; Shastri Nagar Mahesh; Nigam; Sr. Director; 787878878; 30000; Ashok Nagar Manish; Gupta; Director; 999988765; 20000; Saket Nagar
Source: https://www.javatpoint.com/how-to-read-csv-file-in-java