How to parse a CSV file in Java
A comma-separated values (CSV) file is a plain text file that is used to arrange tabular data and is typically found in spreadsheets or databases. CSV files are frequently used as they are accessible in Microsoft Excel, it is very easy to parse data from them, and their data can be imported into a database.
Basic Structure of CSV files
CSV files store data in a particular format where commas separate each data item.
Column 1 , Column 2 , Column 3 ...
Row 1 Data 1 , Row 1 Data 2 , Row 1 Data 3 ...
Row 2 Data 1 , Row 2 Data 2 , Row 2 Data 3 ...
The first row represents the name of the column, and each following row represents the data.
Note: The most frequently used delimiter is a comma. However, other delimiters such as colons, semi-colons, etc. may also be used.
Parse CSV files in Java
In Java, there are multiple ways of reading and parsing CSV files. The most common ones are:
- Using java.util.Scanner
- Using String.split() function
- Using 3rd Party libraries like OpenCSV
In this shot, we will be only looking at the first two methods as they involve built-in libraries.
1. Using java.util.Scanner
A Scanner in Java can be used to break the input into tokens using delimiters. These tokens are then converted into values of different types using various next-methods.
To use this method, we first need to import the Scanner using the statement import java.util.Scanner.
Name, Age, DepartmentJohn, 26, AccountsMike, 30, HRJames, 29, ITOliver,32, IT
2. Using String.split() function
The String.split() function in Java takes tokens from the given string and turns them into tokens that are based on a provided delimiter as a parameter.
Name, Age, DepartmentJohn, 26, AccountsMike, 30, HRJames, 29, ITOliver,32, IT
Free Resources