FileInputStream close() Method in Java with Examples
FileInputStream class is helpful to read data from a file in the form of a sequence of bytes. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
After any operation to the file, we have to close that file. For that purpose, we have a close method. We will learn that in this article. The FileInputStream.close() method closes this file input stream and releases any system resources associated with the stream.
Return Value: The method does not return any value.
Exception: IOException − If any I/O error occurs.
How to invoke the close() method?
Step 1: Attach a file to a FileInputStream as this will enable us to close the file as shown below as follows:
FileInputStream fileInputStream =new FileInputStream(“file.txt”);
Step 2: To close the file, we have to call the close() method using the above instance.
1. We will first read a file and then close it.
2. After closing a file, we will again try to read it.
GeeksforGeeks is a computer science portal File is Closed Now we will again try to read File is closed. Cannot be read java.io.IOException: Stream Closed at java.base/java.io.FileInputStream.read0(Native Method) at java.base/java.io.FileInputStream.read(Unknown Source) at abc.main(abc.java:28)
Note: This code will not run on an online IDE as it requires a file on the system.