How to detect EOF in Java?
It keeps running because it hasn't encountered EOF. At end of stream:
read()
returns -1.read(byte[])
returns -1.read(byte[], int, int)
returns -1.readLine()
returns null.readXXX()
for any other X throwsEOFException
.Scanner.hasNextXXX()
returns false for any X.Scanner.nextXXX()
throwsNoSuchElementException
for any X.
Unless you've encountered one of these, your program hasn't encountered end of stream. NB \u001a
is a Ctrl/z. Not EOF. EOF is not a character value.
This is what I did
Scanner s = new Scanner(f); //f is the file object
while(s.hasNext())
{
String ss = s.nextLine();
System.out.println(ss);
}
Worked for me