In Java, the end-of-file (EOF) character is represented by the EOF
constant of the java.io.BufferedReader
class. This constant is a special value that is used to indicate the end of the input stream when reading from a file or other input source.
To use the EOF
constant, you can use a BufferedReader
object to read characters from an input stream, and check for the EOF
constant when the end of the stream is reached.
Here's an example of using a BufferedReader
to read characters from a file and check for the EOF
constant:
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Main { public static void main(String[] args) { try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) { int ch; while ((ch = reader.read()) != BufferedReader.EOF) { // process the character } } catch (IOException e) { e.printStackTrace(); } } }
In this example, the BufferedReader
reads characters from the file input.txt
one at a time, and the while
loop continues until the EOF
constant is encountered, at which point the loop terminates.
Note that the EOF
constant is a static field of the BufferedReader
class, so you need to use the class name (BufferedReader
) to access it.