Java.io.FileInputStream.close() Method

Advertisements


Description

The java.io.FileInputStream.close() closes this file output stream and releases any system resources associated with this stream.

Declaration

Following is the declaration for java.io.FileInputStream.close() method:

public void close()

Parameters

  • NA

Return Value

The method does not return any value.

Exception

  • NA

Example

The following example shows the usage of java.io.FileInputStream.close() method.

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;

public class FileInputStreamDemo {
   public static void main(String[] args) throws IOException {
      
      FileOutputStream fos = null;
      
      try{
         // create new file output stream
         fos = new FileOutputStream("C://text.txt");
         
         // close stream
         fos.close();
         
         // try to write into underlying stream
         fos.write(65);
         fos.flush();
         fos.close();
   
      }catch(Exception ex){
         // if any error occurs
         System.out.print("IOException: File output stream is closed");
      }finally{
         
         // releases all system resources from the streams
         if(fos!=null)
            fos.close();
      }
   }
}

Assuming we have a text file c:/test.txt, which has the following content. This file will be used as an input for our example program:

ABCDEF

Let us compile and run the above program, this will produce the following result:

IOException: File output stream is closed



Advertisements
Advertisements