GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java - CharArrayReader reset() method
Selected Reading
Java - CharArrayReader reset() methodDescriptionThe Java CharArrayReader reset() method moves the read position back to the mark set by mark(int readAheadLimit). If mark() was not called before reset(), it resets the reader to the beginning of the character array. DeclarationFollowing is the declaration for java.io.CharArrayReader.reset() method − public void reset() ParametersNA Return ValueThe method does not return any value. ExceptionIOException − If any I/O error occurs. Example - Using CharArrayReader reset() methodThe following example shows the usage of Java CharArrayReader reset() method. CharArrayReaderDemo.javapackage com.tutorialspoint;
import java.io.CharArrayReader;
import java.io.IOException;
public class CharArrayReaderDemo {
public static void main(String[] args) {
CharArrayReader car = null;
char[] ch = {'A', 'B', 'C', 'D', 'E'};
try {
// create new character array reader
car = new CharArrayReader(ch);
int value = 0;
// read till the end of the stream
while((value = car.read())!=-1) {
// convert integer to char
char c = (char)value;
// print characters
System.out.print(c + " ");
}
// reset invoked
car.reset();
System.out.println("\nReset() invoked");
// read till the end of the stream
while((value = car.read())!=-1) {
// convert integer to char
char c = (char)value;
// print characters
System.out.print(c + " ");
}
} catch(IOException e) {
// if I/O error occurs
e.printStackTrace();
} finally {
// releases any system resources associated with the stream
if(car!=null)
car.close();
}
}
}
OutputLet us compile and run the above program, this will produce the following result − A B C D E Reset() invoked A B C D E Example - Resetting to the Beginning (No Mark Used)The following example shows the usage of Java CharArrayReader reset() method. CharArrayReaderDemo.javapackage com.tutorialspoint;
import java.io.CharArrayReader;
import java.io.IOException;
public class CharArrayReaderDemo {
public static void main(String[] args) {
char[] data = "Hello, World!".toCharArray();
try (CharArrayReader reader = new CharArrayReader(data)) {
// Read and print first 5 characters
for (int i = 0; i < 5; i++) {
System.out.print((char) reader.read());
}
// Reset to the beginning
reader.reset();
System.out.println("\nAfter reset:");
// Read again from the beginning
int charData;
while ((charData = reader.read()) != -1) {
System.out.print((char) charData);
}
} catch (IOException e) {
System.err.println("IOException occurred: " + e.getMessage());
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Hello After reset: Hello, World! Explanation
Example - Using mark() and reset() to Return to a Specific PositionThe following example shows the usage of Java CharArrayReader reset() method. CharArrayReaderDemo.javapackage com.tutorialspoint;
import java.io.CharArrayReader;
import java.io.IOException;
public class CharArrayReaderDemo {
public static void main(String[] args) {
char[] data = "Java Programming".toCharArray();
try (CharArrayReader reader = new CharArrayReader(data)) {
// Read and print first 5 characters
for (int i = 0; i < 5; i++) {
System.out.print((char) reader.read());
}
// Mark the current position
reader.mark(0);
System.out.println("\nMark set after reading 'Java '.");
// Read the next 6 characters
for (int i = 0; i < 6; i++) {
System.out.print((char) reader.read());
}
// Reset back to the marked position
reader.reset();
System.out.println("\nAfter reset:");
// Read again from the marked position
int charData;
while ((charData = reader.read()) != -1) {
System.out.print((char) charData);
}
} catch (IOException e) {
System.err.println("IOException occurred: " + e.getMessage());
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Java Mark set after reading 'Java '. Progra After reset: Programming Explanation
Key Takeaways
These examples show how reset() can rewind reading positions efficiently in CharArrayReader! java_io_chararrayreader.htm Advertisements
|
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |