It keeps running because it hasn't encountered EOF. At end of stream:

  1. read() returns -1.
  2. read(byte[]) returns -1.
  3. read(byte[], int, int) returns -1.
  4. readLine() returns null.
  5. readXXX() for any other X throws EOFException.
  6. Scanner.hasNextXXX() returns false for any X.
  7. Scanner.nextXXX() throws NoSuchElementException 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.

Answer from user207421 on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 40127546 › how-to-check-the-end-of-a-file-in-java
how to check the end of a file in java - Stack Overflow
import java.io.File; import java.util.Scanner; /** * * @author root */ public class ReadFileScanner { public static void main(String s[]) { String log = "/tmp/ws/prakash-rcd/label" + "prakash_label_test" + ".log"; File file = new File(log); String line = null; boolean logFailureMsg = true; try { Scanner scanner = new Scanner(file); while(scanner.hasNextLine()) { line = scanner.nextLine(); System.out.println("come inside loop to check end of file: line: " + line); if (line.contains("script ran successfully")) { System.out.println("line found.go out of loop now"); break; } else { if(logFailureMsg) { System.out.println("label update faild"); logFailureMsg = false; } } } } catch(Exception e) { e.printStackTrace(); } } }
🌐
Coderanch
coderanch.com › t › 404516 › java › find-file-java
How to find end of file in java (Beginning Java forum at Coderanch)
August 21, 2006 - If you want to write at the end of the file, then FileWriter and FileOutputStream both have constructors that allow you to ask to append to the file. Similarly, you can seek() to the end of a RandomAccessFile, then write to it. Betty Rubble? Well, I would go with Betty... but I'd be thinking ...
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Detect End Of File (EOF) in a Java File - Java Code Geeks
February 19, 2024 - We use a while loop to iterate through each line in the file until the end of the file is reached (EOF). The hasNextLine() method checks if there is another line in the file, and returns false when EOF is reached.
🌐
RoseIndia
roseindia.net › tutorial › java › core › files › fileEndOfFile.html
Java check end of file
In the given example, we have used FileReader and BufferedReader class to read the file till the end of the file. ... import java.io.*; public class FileEOF { public static void main(String[] args) throws Exception { FileReader reader = new FileReader("C:/file.txt"); BufferedReader br = new BufferedReader(reader); String str = ""; while ((str = br.readLine()) != null) { System.out.println(str); } br.close(); } }
🌐
Quora
quora.com › How-do-I-detect-EOF-in-Java
How to detect EOF in Java - Quora
Java does not expose a single EOF flag; instead you detect EOF by observing that read methods return a sentinel (usually -1) or that a token/line method indicates no more data. Common approaches: ... Returns an int 0–255 for a byte, or -1 at EOF.
🌐
Coderanch
coderanch.com › t › 274770 › java › check-EOF-Java
How to check EOF in Java?? (I/O and Streams forum at Coderanch)
They can be constructed on any stream, File, the screen, the keyboard, a pipe between applcations, a network socket etc. Give an example of your code an I might be able to help some more. ... Hi Carl, I am trying to read using Reader objects. That's why I think I am not able to read EOF. Only Streams Methods throw EOFexception when end of file occurs.
🌐
Baeldung
baeldung.com › home › java › java io › detect eof in java
Detect EOF in Java | Baeldung
January 8, 2024 - For EOF detection, we’ll use these two classes – FileChannel to read the file and ByteBuffer to store the results. The approach we use is to read the buffer until it returns the value -1, which indicates the end of the file (EOF):
🌐
Medium
medium.com › @syedmuhammadwaleed71 › how-to-detect-eof-in-java-d3abd1d6875f
How to Detect EOF in Java
September 26, 2022 - How to Detect EOF in Java In this tutorial, we’ll be discussing how to detect EOF (end of file) using a while loop in Java. We’ll also discuss developing a program that continues reading content …
Find elsewhere
🌐
GitHub
gist.github.com › goxr3plus › 6923c59b008113d671e06e2787bccd99
Detect END OF LINE (EOL) in File with Java · GitHub
Save goxr3plus/6923c59b008113d671e06e2787bccd99 to your computer and use it in GitHub Desktop. Download ZIP · Detect END OF LINE (EOL) in File with Java · Raw · Detect END OF LINE (EOL) in File with Java · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
CodingTechRoom
codingtechroom.com › question › detect-eof-java
How to Detect End of File (EOF) in Java - CodingTechRoom
import java.io.*; // Example to detect EOF using BufferedReader public class EOFDetection { public static void main(String[] args) { try (BufferedReader br = new BufferedReader(new FileReader("example.txt"))) { String line; while ((line = br.readLine()) != null) { System.out.println("Read line: " + line); } System.out.println("End of file reached."); } catch (IOException e) { e.printStackTrace(); } } }
🌐
HackerRank
hackerrank.com › challenges › java-end-of-file › forum
Java End-of-file Discussions | Java | HackerRank
*/ Scanner in = new Scanner(System.in); int i=1; String str = ""; while(in.hasNext()){ str += "" + (i++) + " " + in.nextLine() + "\n"; } System.out.println(str); } ... All recent soluitions I see here seem to ignore the requirement of reading ...
🌐
Experts Exchange
experts-exchange.com › questions › 27462968 › JAVA-How-to-read-in-end-of-file-from-the-KEYBOARD.html
Solved: JAVA: How to read in end of file from the KEYBOARD? | Experts Exchange
November 24, 2011 - But you need to make ctrl-Z (it wil print ctrl-Z on the cmd) and then carriage return then it will exit out of the loop ... import java.util.Scanner; public class Miscellaneous { public static void main(String[] args) { String input = null; ...
🌐
Post.Byes
post.bytes.com › home › forum › topic › java
how to detect end of file in java - Post.Byes
i have a text file and i have to print all values using file io system.. can you tell me how to check end of file.. thanks in advance. All methods return an appropriate sentinel value such as -1 or null; read the API documentation for the classes and methods you want to use.
🌐
DEV Community
dev.to › ezzeddinp › read-input-until-eof-end-of-file-and-number-your-lines-effortlessly-competitive-programming-java-1egb
Read Input Until EOF (End-of-File) and Number Your Lines Effortlessly | Competitive Programming Java - DEV Community
September 1, 2024 - 1 Hello world 2 I am a file 3 Read me until end-of-file. Java provides a handy way to handle such problems with the Scanner class and its hasNext() method. This method allows us to read input until there's no more data to read (EOF).
🌐
CodingTechRoom
codingtechroom.com › tutorial › java-detecting-end-of-file-java
Detecting End of File in Java: A Comprehensive Guide - CodingTechRoom
Using BufferedReader and InputStream ... read from a file or stream. Q. Can I detect EOF without reading all data? A. Yes, using methods like FileChannel can allow checking file position and EOF....
🌐
javaspring
javaspring.net › blog › determine-end-of-file-using-java-s-scanner-class
How to Determine End-of-File (EOF) in Java Using Scanner Class: A Step-by-Step Guide — javaspring.net
EOF is not a physical character in a file or stream; it is a signal indicating that the end of the input stream has been reached. When a program reads data, it continues until it encounters this signal, after which no more data can be read. Prevents runtime exceptions (e.g., NoSuchElementException when Scanner tries to read beyond available data). Ensures the program processes all input correctly and terminates gracefully. Avoids infinite loops or partial data processing. The java.util.Scanner class is a versatile tool for parsing input.
🌐
Blogger
javaispapa.blogspot.com › 2017 › 07 › java-end-of-file.html
Java Tutorial: Java End-of-file
July 29, 2017 - "In computing, End Of File (commonly abbreviated EOF ) is a condition in a computer operating system where no more data can be read...
🌐
Delft Stack
delftstack.com › home › howto › java › detect eof in java
How to Detect EOF in Java | Delft Stack
February 2, 2024 - This output demonstrates the successful detection of EOF using the InputStream.read() method. The program reads each byte from the file and prints it until the end of the file is reached, providing an effective way to handle EOF scenarios during ...
🌐
Code with Mosh
forum.codewithmosh.com › java
Eof file in java - Java - Code with Mosh Forum
September 19, 2022 - I tried to read lines of input until you reach EOF, then number and print all lines of content. Sample Input Hello world I am a file Read me until end-of-file. Sample Output 1 Hello world 2 I am a file 3 Read me until end-of-file. my code: Scanner sc= new Scanner(System.in); String msg= sc.next(); byte n=1; n= sc.nextByte(); while(true){ System.out.println(n+" “+sc.hasNext(”/0")); n++; } im getting error…can anyone help me with this