I need to read a text file line by line using Java. I use available()
method of FileInputStream
to check and loop over the file. But while reading, the loop terminates after the line before the last one. i.e., if the file has 10 lines, the loop reads only the first 9 lines.
Snippet used :
while(fis.available() > 0)
{
char c = (char)fis.read();
.....
.....
}
Try this just a little search in Google
How about using Scanner? I think using Scanner is easier
Read more about Java IO here
Yes, buffering should be used for better performance. Use BufferedReader OR byte[] to store your temp data.
thanks.
This worked for me
The reason your code skipped the last line was because you put
fis.available() > 0
instead offis.available() >= 0