Java BufferedInputStream close() Example

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


Syntax is : public void close() throws IOException
This method closes this input stream and releases any system resources associated with the stream. Once the stream has been closed, further read(), available(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.

Here is the code.
/**
 * @(#) CloseBufferedInputStream.java
 * A class representing use of method close() of BufferedInputStream class in 
 java.io Package.
 * @Version  03-May-2008
 @author   Rose India Team
 */
import java.io.*;
public class CloseBufferedInputStream {
     public static void main(String[] args) {
       
     try{
       /* Construct an buffer input stream using some String value.
      This is deprecated method in java api 1.6. */
        StringBufferInputStream obj = new StringBufferInputStream("MAHENDRA")

    // Construct the buffer using the buffer input stream obj.
        BufferedInputStream bin = new BufferedInputStream(obj);

        // read() method read elements one by one from the input stream buffer:
        System.out.println("First element in buffer is : "+(char)bin.read());
    System.out.println("Next element in buffer is  : "+(char)bin.read());
    System.out.println("Next element in buffer is  : "+(char)bin.read());
    System.out.println("Next element in buffer is  : "+(char)bin.read());
        
        System.out.println("close() method call.");
    bin.close();
    System.out.print((char)bin.read());
    }
    catch Exception e){
      System.out.println("buffer can not read because buffer is closed now.");
      }       
   }
}

Output of the program.
First element in buffer is : M
Next element in buffer is  : A
Next element in buffer is  : H
Next element in buffer is  : E
close() method call.
buffer can not read because buffer is closed now.

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

HOME | COPYRIGHT | CONTACT US