PDA

Click to See Complete Forum and Search --> : reading from file[Resolved]


System_Error
May 21st, 2005, 03:20 PM
How can you read a certain line from a file without wasting your time processing each line like this:

try {
BufferedReader in = new BufferedReader(new FileReader("infilename"));
String str;
while ((str = in.readLine()) != null) {
process(str);
}
in.close();
} catch (IOException e) {}



Like, if I had a list of 5000 words, and I wanted to skip to the 1234th word, how would I do that? I know I could use a counting variable in the above example, but I wouldn't want to keep reading everyline like that untill I got to the right one.

Dillinger4
May 21st, 2005, 03:34 PM
Perhaps java.io.LineNumberReader?. Maybe you can use setLineNumber(int lineNumber) to set the line number then readLine() to read that exact line.

Dillinger4
May 21st, 2005, 03:36 PM
Nope sorry. According to the docs.

Note however, that setLineNumber(int) does not actually change the current position in the stream; it only changes the value that will be returned by getLineNumber().

System_Error
May 23rd, 2005, 05:05 AM
Thanks. Actually, I think what I was going to do was a bad idea. Instead, I read all 5000 words from the file into an arraylist, at runtime. That way I didn't have to deal with the file anymore, and that worked out well.