Read Values From Csv File in Java
How to read and parse CSV file in Java? Do we take any built in Java utility which converts CSV (Comma Separated Values) Cord to ArrayList object? The answer is NO. But it'south not a big trouble. With below unproblematic utility y'all could catechumen CSV to ArrayList without any issue.
What is CSV file?
A CSV is a comma separated values
file, which allows data to exist 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 accept exported top Keywords in CSV format from Webmaster Tool. Google Spreadsheets and Microsoft Excel brand it like shooting fish in a barrel to edit those generatedCSV files. Your CSV file should be formatted equally a table and must include a header, or first line, that defines the fields in your table.
Permit's get started
Step-1
Create a sample text file and name it every bitCrunchify-CSV-to-ArrayList.txt
. Copy and Paste below content into file.
Crunchify , Web Development , NYC , five Employees Google , Search Company , Mount View , 53600 Employees Yahoo , News Company , Sunnyvale , 12500 Employees Microsoft , Windows Visitor , Washington , 128000 Employees |
Step-2
- How to read file in Java line by line? We will starting time read above file in Java using elementary
java.io.BufferedReader
- Create
crunchifyCSVtoArrayList(String)
utility which converts CSV to Arraylist - Use
java.lang.String.separate(String regex)
utility
ane 2 3 4 five 6 7 eight 9 ten 11 12 13 14 15 xvi 17 18 xix twenty 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 twoscore 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | package crunchify . com . tutorial ; import java . io . BufferedReader ; import java . io . FileReader ; import java . io . IOException ; import coffee . util . ArrayList ; /** * @author Crunchify.com * */ public class CrunchifyCSVtoArrayList { public static void master ( Cord [ ] args ) { BufferedReader crunchifyBuffer = cipher ; 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 information: " + crunchifyLine ) ; System . out . println ( "Converted ArrayList data: " + crunchifyCSVtoArrayList ( crunchifyLine ) + "\northward" ) ; } } catch ( IOException e ) { e . printStackTrace ( ) ; } finally { try { if ( crunchifyBuffer != nada ) crunchifyBuffer . close ( ) ; } grab ( IOException crunchifyException ) { crunchifyException . printStackTrace ( ) ; } } } // Utility which converts CSV to ArrayList using Split Operation public static ArrayList <String> crunchifyCSVtoArrayList ( Cord crunchifyCSV ) { ArrayList <String> crunchifyResult = new ArrayList <String> ( ) ; if ( crunchifyCSV != null ) { Cord [ ] splitData = crunchifyCSV . dissever ( "\\due south*,\\s*" ) ; for ( int i = 0 ; i < splitData . length ; i++) { if ( ! ( splitData [ i ] == nada ) | | ! ( splitData [ i ] . length ( ) == 0 ) ) { crunchifyResult . add ( splitData [ i ] . trim ( ) ) ; } } } return crunchifyResult ; } } |
We are using regex\\south*,\\due south*
.
\s
matches any whitespace, The *
applies the match zero or more times. And then \southward*
means "match whatsoever white space zero or more times". We look for this before and after the comma. Therefore, the split volition work for strings like "company1 ,company2 , company3"
, or "company1,company2,company3"
, etc. In Coffee, you need to escape the backslash in strings, then y'all get \\s*.
Result:
Raw CSV data : Crunchify , Web Development , NYC , 5 Employees Converted ArrayList information : [ Crunchify , Web Development , NYC , five Employees ] Raw CSV data : Google , Search Company , Mountain View , 53600 Employees Converted ArrayList data : [ Google , Search Visitor , Mountain View , 53600 Employees ] Raw CSV data : Yahoo , News Visitor , 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 ] |
Source: https://crunchify.com/how-to-read-convert-csv-comma-separated-values-file-to-arraylist-in-java-using-split-operation/
0 Response to "Read Values From Csv File in Java"
Post a Comment