1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
/* Parse CSV File using StringTokenizer example. This example shows how to parse comma separated file (CSV file) using Java StringTokenizer and BufferedReader classes. */ import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.util.StringTokenizer; public class ParseCSVFileExample { public static void main(String[] args) { try { //csv file containing data String strFile = "C:/FileIO/example.csv"; //create BufferedReader to read csv file BufferedReader br = new BufferedReader( new FileReader(strFile)); String strLine = ""; StringTokenizer st = null; int lineNumber = 0, tokenNumber = 0; //read comma separated file line by line while( (strLine = br.readLine()) != null) { lineNumber++; //break comma separated line using "," st = new StringTokenizer(strLine, ","); while(st.hasMoreTokens()) { //display csv values tokenNumber++; System.out.println("Line # " + lineNumber + ", Token # " + tokenNumber + ", Token : "+ st.nextToken()); } //reset token number tokenNumber = 0; } } catch(Exception e) { System.out.println("Exception while reading csv file: " + e); } } } /* Input csv file "one","two","three","four" "parsing","comma","separated","file","java","example" */ /* Output would be, Line # 1, Token # 1, Token : "one" Line # 1, Token # 2, Token : "two" Line # 1, Token # 3, Token : "three" Line # 1, Token # 4, Token : "four" Line # 2, Token # 1, Token : "parsing" Line # 2, Token # 2, Token : "comma" Line # 2, Token # 3, Token : "separated" Line # 2, Token # 4, Token : "file" Line # 2, Token # 5, Token : "java" Line # 2, Token # 6, Token : "example" */ |
Great easy to follow example, thanks!
Why reset the tokenNumber to 0?
Thanks for your time!
In line 29, it should be ==, single = is assignment operator.
no it shouldnt, he is setting it equal to the the next line and then the conditional statement is if its null
Thanks so much
Hi,
Thank you for your code. However since I am parsing a very large csv file
and I do some recording in a database every round I got an java.lang.OutOfMemoryError: Java heap space exception.
Do you think it can come from the tokenization or is it due to the recordings?
If you parse huge files you probably want to use some kind of event driven parsing mechanism.
See for example jsapar.tigris.org.
Here you can find a collection of links to available libraries that are related to file parsing:
jsapar.blogspot.com/…
Hi how do i stop the output being enclosed in “”. so instead of for example “output” i just want output. Thanks
use String.replaceAll in that case.
I don’t believe this example would handle the case of embedded, qualified commas:
one”,”two”,”three”,”four”
“parsing”,”Dallas,TX”,”test”
It would..as long as the values are enclosed in the double quotes. Give it a try.
Thank you for your code.
thank you so much
thank you so much
It works perfect. Thanks.
Using StringTokenizer you can’t handle empty “fields” like:
42,,41
Use this library for example ostermiller.org/util…