Java BufferedInputStream reset() Example

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


Syntax is : public void reset() throws IOException
This method resets the buffer to the specified marked position, If mark position is -1 (no mark has been set or the mark has been invalidated), an Exception is thrown. Otherwise, pos is set equal to mark position.

Here is the code.
/**
 * @(#) ResetBufferedInputStream.java
 * A class representing use of method reset() of BufferedInputStream class in 
 java.io Package.
 * @Version  03-May-2008
 @author   Rose India Team
 */
import java.io.*;
public class ResetBufferedInputStream {
     public static void main(String[] argsthrows IOException {
       
       /* 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());

    // element at first position is marked.
    bin.mark(0);
    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("reset() method call so next element read will be "+
      "first element .");

    /* reset() method call.Method resets the buffer to the next element of 
      the marked element. */
    bin.reset();
    System.out.println("Next element in buffer is  : "+(char)bin.read());
    System.out.println("Next element in buffer is  : "+(char)bin.read());
   }
}

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
reset() method call so next element read will be first element .
Next element in buffer is  : A
Next element in buffer is  : H

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

HOME | COPYRIGHT | CONTACT US