Java CharArrayReader reset() Example
CharArrayReader class reset() method example. This example shows you how to use reset() method.
Syntax is : public void reset() throws IOException
This method resets the stream to the most recent mark, or to the beginning if it has never been marked.
Here is the code.
/**
* @(#) ResetCharArrayReader.java
* A class representing use of method reset() of CharArrayReader
class in java.io Package.
* @Version 31-May-2008
* @author Rose India Team
*/
import java.io.*;
public class ResetCharArrayReader {
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.
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("next letter is : "+(char)obj.read());
System.out.println("next letter is : "+(char)obj.read());
// Reset the CharArrayReader.
obj.reset();
System.out.println("Character buffer has reset now so next element "
+"will start from first element.");
System.out.println("next letter is : "+(char)obj.read());
System.out.println("next letter is : "+(char)obj.read());
// Set the mark,The 0th element is ignored.
obj.mark(1);
// Continue reading from the array.
System.out.println("next letter is : "+(char)obj.read());
System.out.println("next letter is : "+(char)obj.read());
System.out.println("next letter is : "+(char)obj.read());
// Reset the CharArrayReader.
obj.reset();
System.out.println("Character buffer has again reset now so that "+
"stream will reset \nto the most recent marked element.");
// Get the first character read after the reset.
char nextLetter = (char)obj.read();
System.out.println("The element 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
next letter is : A
next letter is : H
Character buffer has reset now so next element will start from first element.
next letter is : M
next letter is : A
next letter is : H
next letter is : E
next letter is : N
Character buffer has again reset now so that stream will reset
to the most recent marked element.
The element read after reset is: H |
|