Java BufferedInputStream mark() Example
BufferedInputStream class mark() method example. This example shows you how to use mark() method.
Syntax is : public void mark(int readlimit)
This method marks the specified element in the input stream buffer. Parameter readlimit is the maximum limit of bytes that can be read before the mark position becomes invalid.
Here is the code.
/**
* @(#) MarkBufferedInputStream.java
* A class representing use of method mark(() of BufferedInputStream class in
java.io Package.
* @Version 03-May-2008
* @author Rose India Team
*/
import java.io.*;
public class MarkBufferedInputStream {
public static void main(String[] args) throws 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 from the input stream buffer.
System.out.println("\nFirst element is : "+(char)bin.read());
System.out.println("Next element is : "+(char)bin.read());
// Set the mark position for second element.
bin.mark(1);
System.out.println("Element at second position has marked.");
System.out.println("Next element is : "+(char)bin.read());
System.out.println("Next element is : "+(char)bin.read());
// Reset the BufferedInputStream.
bin.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)bin.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 |
|