Java FileReader ready() Method
Example
Read characters from a file as long as the reader is ready:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
FileWriter myWriter = new FileWriter("filename.txt");
myWriter.write("Hello World");
myWriter.close();
FileReader myReader = new FileReader("filename.txt");
while (myReader.ready()) {
System.out.print((char) myReader.read());
}
myReader.close();
}
}
Definition and Usage
The ready() method checks if the reader is ready. This means that the next read will not have to wait for input.
For a file, it returns true as long as there are more characters to read. But false does not always mean the end of the input, so the safe way to find the end is to check if read() returns -1.
Syntax
public boolean ready()
Parameter Values
None.
Technical Details
| Returns: | true if the next read will not have to wait for input, otherwise false. |
|---|---|
| Throws: | IOException - if the reader is closed, or if another I/O error occurs. |
Related Pages
❮ FileReader Methods