Java How to Open a File Again After Closing It
How to Read CSV File in Java
The CSV stands for Comma-Separated Values. Information technology is a simple file format which is used to store tabular data in simple text grade, 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 split different information token in a file. The CSV file format is used when we move tabular data between programs that natively operate on incompatible formats. At that place are following ways to read CSV file in Coffee. 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 ii ways to create a CSV file:
- Using Microsoft Excel
- Using Notepad
Using Microsoft Excel
Stride 1: Open up 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 equally blazon carte du jour. Now, click on the Save button.

Using Notepad
Stride ane: Open notepad.
Pace ii: Write some information into file separated past comma (,). For example:
Vivek, Singh, 23, 9999999, Chandigarh
Pace three: Save the file with .csv extension.
Nosotros have created the following file.

Java Scanner class
Java Scanner class provide various methods by which we can read CSV file. The Scanner form provides a constructor that produces values scanned from the specified file. It breaks information into the token class. It uses a delimiter pattern which past default matches white space. The resulting tokens and so converted into values of different types using the next() methods.
Example
Output:
Shashank, Mishra, Accountant, 909090090, 45000, Moti Vihar Naveen, Singh, Accountant, 213344455, 12000, Shastri Nagar Mahesh, Nigam, Sr. Managing director, 787878878, 30000, Ashok Nagar Manish, Gupta, Manager, 999988765, 20000, Saket Nagar
Java Cord.dissever() method
Java Cord.split up() identifies the delimiter and divide the rows into tokens.
Syntax
The method parses a delimiting regular expression. The method returns an assortment of string computed past splitting this cord around matches of the given regular expression.
Consider the cord:
"this:is:a:table" Regex Result : {"this", "is", "a", "table"}
Case
In the following example, nosotros use BufferedReader class which reads file line past line until the EOF (end of file) character is reached.
Output:
Employee [Kickoff Name= Shashank, Terminal Proper noun= Mishra, Designation= Auditor, Contact= 909090090, Bacon= 45000, Urban center= Moti Vihar] Employee [First Proper noun= Naveen, Last Name=Singh, Designation= Accountant, Contact=213344455, Salary= 12000, City= Shastri Nagar] Employee [First Proper name= Mahesh, Final Proper name=Nigam, Designation= Sr. Managing director, Contact=787878878, Salary= 30000, Urban center= Ashok Nagar] Employee [First Name= Manish, Last Proper name=Gupta, Designation= Manager, Contact=999988765, Salary= 20000, City= Saket Nagar]
Using OpenCSV API
OpenCSV is a third political party API which provide standard libraries to read diverse versions of CSV file. The library provides better control to handle the CSV file. The library tin 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 bridge multiple lines.
The CSVReader course 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: Information technology is a delimiter which is used for separating entries.
Steps to read CSV file in eclipse:
Stride 1: Create a class file with the name ReadCSVExample3 and write the following lawmaking.
Step 2: Create a lib folder in the project.
Footstep 3: Download opecsv-3.8.jar from
https://repo1.maven.org/maven2/com/opencsv/opencsv/3.8/opencsv-3.8.jar
Step iv: Copy the opencsv-3.viii.jar and paste into the lib folder.
Footstep 5: Now, run the program.
Instance
Output:
Shashank Mishra Auditor 909090090 45000 Moti Vihar Naveen Singh Accountant 213344455 12000 Shastri Nagar Mahesh NigamSr. Managing director 787878878 30000 Ashok Nagar Manish Gupta Manager 999988765 20000 Saket Nagar
Reading CSV file with a unlike separator
In the post-obit CSV file, we take used semicolon (;) to split tokens.

Example
Output:
Shashank; Mishra; Auditor; 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
Post a Comment for "Java How to Open a File Again After Closing It"