Java code to read comma delimited file in fast way ?

kmthien

Senior member
Oct 8, 2002
363
0
0
Hi,

Any java source code that can read/write comma delimited file in a very.....fast way ?

kmthien
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
in perl you can read it into a 2 dimensional array like this:

open F, "input.txt";
while( <F> ){
my @temp = split /,/;
push @data, \@temp;
}
close F;

Which is absolutely no help at all :)
I don't know how to do it in java. I kinda gave up on java - decided I didn't like it.
 

VBboy

Diamond Member
Nov 12, 2000
5,793
0
0
How large is the file? If it can fit into memory entirely, you can read the entire file, then process it. Alternatively, you could read it in chunks.

Now, I'm going back to my VB :)
 

m0ti

Senior member
Jul 6, 2001
975
0
0
Use a string tokenizer:

BufferedReader in = new BufferedReader(new FileReader(fileName));

String line;

while ((line = in.readLine()) != null) {
StringTokenizer sTok = new StringTokenizer(line, ",");

// get each value by sTok.nextToken() and do your processing.

}

Of course, you've also got to handle the IOException thrown by in.readLine(). StringTokenizer is part of java.util.