Java FilterInputStream close() Example

FilterInputStream class close() method example. This example shows you how to use available() method.


Syntax is : public void close() throws IOException
Method closes this input stream and releases any system resources associated with the stream. This method simply performs in.close().

Here is the code.
/**
 * @(#) CloseFilterInputStream.java
 * A class representing use of method close() of FilterInputStream 
 class in java.io Package.
 * @Version  09-June-2008
 @author   Rose India Team
 */
import java.io.*;

public class CloseFilterInputStream extends FilterInputStream {

    CloseFilterInputStream(FileInputStream objFin){
    super(objFin);
    }

  public static void main(String[] argsthrows IOException{

        try {
            byte[] byteArray = new byte[]{'M','A','H','E','N','D','R','A'};

        //Create a file with a specified name and file extension.
        File file = new File("mahendra.txt");

        // Create a output stream and write specified bytes in file.
            FileOutputStream objFileStream = new FileOutputStream(file);
        objFileStream.write(byteArray);

        // create a file input stream to read content of the file.
          FileInputStream objFin = new FileInputStream(file);
        CloseFilterInputStream obj =new CloseFilterInputStream(objFin);

            System.out.print("\nElements read : ");
            for (int i = 0; i < 5; i++) {
                System.out.print((charobj.read());
            }
      // close() method call.
            obj.close();
            System.out.print("\nclose method is called for this input stream.");
            System.out.println((charobj.read());
        }
        catch (Exception ex){
      System.out.println("\nCan't read more elements bacause file stream"
                    +" is closed now.");
        }
    }
}

Output of the program.
 Elements read : MAHEN
close method is called for this input stream.
Can't read more elements bacause file stream is closed now.

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US