How to read and parse CSV file in Java? Do we have any built in Java utility which converts CSV (Comma Separated Values) String to ArrayList object? The answer is NO. But it’s not a big problem. With below simple utility you could convert CSV to ArrayList without any issue.
What is CSV file?
A CSV is a comma separated values
file, which allows data to be saved in a table structured format. If you are using Google Webmaster tool and know how to handle your site’s SEO then you must have exported top Keywords in CSV format from Webmaster Tool. Google Spreadsheets and Microsoft Excel make it easy to edit those generated CSV files. Your CSV file should be formatted as a table and must include a header, or first line, that defines the fields in your table.
Let’s get started
Step-1
Create a sample text file and name it as Crunchify-CSV-to-ArrayList.txt
. Copy and Paste below content into file.
Crunchify,Web Development,NYC,5 Employees Google,Search Company,Mountain View,53600 Employees Yahoo,News Company,Sunnyvale,12500 Employees Microsoft,Windows Company,Washington,128000 Employees
Step-2
- How to read file in Java line by line? We will first read above file in Java using simple
java.io.BufferedReader
- Create
crunchifyCSVtoArrayList(String)
utility which converts CSV to Arraylist - Use
java.lang.String.split(String regex)
utility
package crunchify.com.tutorial; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; /** * @author Crunchify.com * */ public class CrunchifyCSVtoArrayList { public static void main(String[] args) { BufferedReader crunchifyBuffer = null; try { String crunchifyLine; crunchifyBuffer = new BufferedReader(new FileReader("/Users/appshah/Documents/Crunchify-CSV-to-ArrayList.txt")); // How to read file in java line by line? while ((crunchifyLine = crunchifyBuffer.readLine()) != null) { System.out.println("Raw CSV data: " + crunchifyLine); System.out.println("Converted ArrayList data: " + crunchifyCSVtoArrayList(crunchifyLine) + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (crunchifyBuffer != null) crunchifyBuffer.close(); } catch (IOException crunchifyException) { crunchifyException.printStackTrace(); } } } // Utility which converts CSV to ArrayList using Split Operation public static ArrayList<String> crunchifyCSVtoArrayList(String crunchifyCSV) { ArrayList<String> crunchifyResult = new ArrayList<String>(); if (crunchifyCSV != null) { String[] splitData = crunchifyCSV.split("\\s*,\\s*"); for (int i = 0; i < splitData.length; i++) { if (!(splitData[i] == null) || !(splitData[i].length() == 0)) { crunchifyResult.add(splitData[i].trim()); } } } return crunchifyResult; } }
We are using regex \\s*,\\s*
.
\s
matches any whitespace, The *
applies the match zero or more times. So \s*
means “match any white space zero or more times”. We look for this before and after the comma. Therefore, the split will work for strings like "company1 ,company2 , company3"
, or "company1,company2,company3"
, etc. In Java, you need to escape the backslash in strings, so you get \\s*.
Result:
Raw CSV data: Crunchify,Web Development,NYC,5 Employees Converted ArrayList data: [Crunchify, Web Development, NYC, 5 Employees] Raw CSV data: Google,Search Company,Mountain View,53600 Employees Converted ArrayList data: [Google, Search Company, Mountain View, 53600 Employees] Raw CSV data: Yahoo,News Company,Sunnyvale,12500 Employees Converted ArrayList data: [Yahoo, News Company, Sunnyvale, 12500 Employees] Raw CSV data: Microsoft,Windows Company,Washington,128000 Employees Converted ArrayList data: [Microsoft, Windows Company, Washington, 128000 Employees]