Java FileInputStream close() Method
Example
Close a stream, then try to read from it:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
new File("filename.txt").createNewFile();
FileInputStream input = new FileInputStream("filename.txt");
input.close();
try {
input.read();
} catch (IOException e) {
System.out.println("Error: the stream is closed.");
}
}
}
Definition and Usage
The close() method closes the stream, and releases (gives back) the file and the system resources that the stream uses.
Always close a stream when you are done with it. After the stream is closed, you cannot read from it. If you try, an IOException is thrown.
Tip: A try-with-resources statement closes the stream for you: try (FileInputStream input = new FileInputStream("filename.txt")) { }. See the FileInputStream Tutorial.
Syntax
public void close()
Parameter Values
None.
Technical Details
| Throws: | IOException - if the stream cannot be closed. |
|---|
Related Pages
❮ FileInputStream Methods