Java CharArrayReader mark() Example
CharArrayReader class mark() method example. This example shows you how to use mark() method.
Syntax is : public void mark(int readAheadLimit) throws IOException
This method marks the present position in the stream. Subsequent calls to reset() will reposition the stream to this point.
Here is the code
/**
* @(#) MarkCharArrayReader.java
* A class representing use of method mark() of CharArrayReader
class in java.io Package.
* @Version 31-May-2008
* @author Rose India Team
*/
import java.io.*;
public class MarkCharArrayReader {
public static void main(String[] args) throws IOException{
char[] arrBuffer = {'M','A','H','E','N','D','R','A'};
System.out.print("given character buffer is : ");
for (int i=0;i<arrBuffer.length ;i++ ){
System.out.print(arrBuffer[i]);
}
// Create a new CharArrayReader object.
CharArrayReader obj = new CharArrayReader(arrBuffer);
// Read from the underlying array one at a time.
System.out.println("\nFirst letter is : "+(char)obj.read());
System.out.println("Second letter is : "+(char)obj.read());
// Set the mark,The 0th element is ignored.
obj.mark(1);
System.out.println("Second element has marked of the character buffer.");
// Continue reading from the array.
System.out.println("Third letter is : "+(char)obj.read());
System.out.println("Fourth letter is : "+(char)obj.read());
System.out.println("Fifth letter is : "+(char)obj.read());
// Reset the CharArrayReader.
obj.reset();
// Get the first character read after the reset.
char nextLetter = (char)obj.read();
System.out.println("The next letter read after reset is: "+nextLetter);
}
}
/* Another constructor of class CharArrayReader:-
public CharArrayReader(char[] buf, int offset, int length)
Description:
Creates a CharArrayReader from the specified array of chars.
The resulting reader will start reading at the given offset. The total
number of char values that can be read from this reader will be either
length or buf.length-offset, whichever is smaller.
Parameters:
buf - Input buffer (not copied)
offset - Offset of the first char to read
length - Number of chars to read
Throws:
IllegalArgumentException - If offset is negative or greater than
buf.length, or if length is negative, or if the sum of these two values
is negative. */ |
Output of the program.
given character buffer is : MAHENDRA
First letter is : M
Second letter is : A
Second element has marked of the character buffer.
Third letter is : H
Fourth letter is : E
Fifth letter is : N
The next letter read after reset is: H |
|