Java FilterInputStream reset() Example
FilterInputStream class reset() method example. This example shows you how to use reset() method.
Syntax is : public void reset() throws IOException
Method repositions this stream to the position at the time the mark method was last called on this input stream. This method simply performs in.reset().
Here is the code.
/**
* @(#) ResetFilterInputStream.java
* A class representing use of method reset() of FilterInputStream
class in java.io Package.
* @Version 09-June-2008
* @author Rose India Team
*/
import java.io.*;
public class ResetFilterInputStream extends FilterInputStream {
ResetFilterInputStream(InputStream objFin){
super(objFin);
}
public static void main(String[] args) throws IOException{
byte[] byteArrayOut = new byte[]{'M','A','H','E','N','D','R','A'};
/* Create object of ByteArrayOutputStream class to write specified
byte array to the output byte stream. */
ByteArrayOutputStream objByteStream = new ByteArrayOutputStream();
objByteStream.write(byteArrayOut,0,8);
// Create object of ByteArrayInputStream class.
InputStream objInStream = new ByteArrayInputStream(byteArrayOut);
//Create object of MarkSupportedFilterInputStream class.
ResetFilterInputStream obj =new ResetFilterInputStream(objInStream);
// Read from the input stream buffer.
System.out.println("\nFirst element is : "+(char)obj.read());
System.out.println("Next element is : "+(char)obj.read());
// Set the mark position for second element.
obj.mark(1);
System.out.println("Element at second position has marked.");
System.out.println("Next element is : "+(char)obj.read());
System.out.println("Next element is : "+(char)obj.read());
// Reset the BufferedInputStream.
obj.reset();
System.out.println("reset() method call so next element read will be "
+ "next element to the marked element.");
// read character after the reset the buffer.
char nextLetter = (char)obj.read();
System.out.println("The next element read after reset is: "+nextLetter);
}
} |
Output of the program.
First element is : M
Next element is : A
Element at second position has marked.
Next element is : H
Next element is : E
reset() method call so next element read will be next element to the marked element.
The next element read after reset is: H |
|