Read to Arraylist Line by Line Java
How to read and parse CSV file in Coffee? Practice we accept any built in Java utility which converts CSV (Comma Separated Values) String to ArrayList object? The reply is NO. But it's not a big problem. With beneath 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 and then you must have exported top Keywords in CSV format from Webmaster Tool. Google Spreadsheets and Microsoft Excel make information technology easy to edit those generatedCSV files. Your CSV file should be formatted as a tabular array and must include a header, or get-go line, that defines the fields in your table.
Permit's get started
Pace-1
Create a sample text file and name information technology asCrunchify-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-two
- How to read file in Java line past line? We volition first read above file in Coffee using unproblematic
java.io.BufferedReader - Create
crunchifyCSVtoArrayList(String)utility which converts CSV to Arraylist - Use
coffee.lang.String.split(String regex)utility
| 1 2 iii 4 5 6 7 8 ix 10 11 12 13 xiv 15 sixteen 17 eighteen 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 forty 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | parcel crunchify . com . tutorial ; import java . io . BufferedReader ; import coffee . io . FileReader ; import java . io . IOException ; import coffee . util . ArrayList ; /** * @author Crunchify.com * */ public course CrunchifyCSVtoArrayList { public static void main ( String [ ] args ) { BufferedReader crunchifyBuffer = cipher ; endeavor { Cord 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 ) { east . printStackTrace ( ) ; } finally { attempt { if ( crunchifyBuffer != null ) crunchifyBuffer . close ( ) ; } catch ( IOException crunchifyException ) { crunchifyException . printStackTrace ( ) ; } } } // Utility which converts CSV to ArrayList using Split Operation public static ArrayList <Cord> crunchifyCSVtoArrayList ( String crunchifyCSV ) { ArrayList <String> crunchifyResult = new ArrayList <Cord> ( ) ; if ( crunchifyCSV != naught ) { String [ ] splitData = crunchifyCSV . carve up ( "\\due south*,\\s*" ) ; for ( int i = 0 ; i < splitData . length ; i++) { if ( ! ( splitData [ i ] == zippo ) | | ! ( splitData [ i ] . length ( ) == 0 ) ) { crunchifyResult . add ( splitData [ i ] . trim ( ) ) ; } } } return crunchifyResult ; } } |
We are using regex\\s*,\\s*.
\south matches any whitespace, The * applies the match nix or more times. So \s* means "lucifer any white space nil or more times". We look for this earlier and afterward the comma. Therefore, the split up will work for strings like "company1 ,company2 , company3", or "company1,company2,company3", etc. In Java, you need to escape the backslash in strings, so yous get \\southward*.
Result:
| Raw CSV data : Crunchify , Web Development , NYC , v Employees Converted ArrayList data : [ Crunchify , Web Development , NYC , 5 Employees ] Raw CSV information : Google , Search Visitor , Mountain View , 53600 Employees Converted ArrayList data : [ Google , Search Company , Mount View , 53600 Employees ] Raw CSV information : Yahoo , News Visitor , Sunnyvale , 12500 Employees Converted ArrayList data : [ Yahoo , News Visitor , Sunnyvale , 12500 Employees ] Raw CSV information : Microsoft , Windows Company , Washington , 128000 Employees Converted ArrayList information : [ Microsoft , Windows Visitor , 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 to Arraylist Line by Line Java"
Post a Comment