Reputation: 2106
Is there a way to count the number of lines in a file, in java, if the file was declared as a random access file? I was thinking of sth like:
while(stop_condition)
{String str=file.readLine();
count++;
}
But i can't find in the random access file documentation, what could be in this case the stop_condition. Could you please help me out. Thank you.
Upvotes: 1
Views: 3469
Reputation: 49097
String line = null;
while ((line = file.readLine() != null) {
// Do something :)
}
Upvotes: 1
Reputation: 12623
Documentation was the first result in google: http://docs.oracle.com/javase/1.4.2/docs/api/java/io/RandomAccessFile.html
Here's what it says to do:
while(file.readLine() != null)
count++;
Upvotes: 3
Reputation: 14544
file.readLine()
will return null when you reach the end of the file.
Upvotes: 1