Java FileReader close() Method
Example
Read the first character of a file, then close the reader:
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");
System.out.println((char) myReader.read());
myReader.close();
}
}
Definition and Usage
The close() method closes the reader, so the file is released (other programs can use it again).
After you close a reader, you cannot read from it anymore. Methods like read() then throw an IOException.
Tip: Use try-with-resources to close the reader automatically, even if an error occurs.
Syntax
public void close()
Parameter Values
None.
Related Pages
❮ FileReader Methods