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 - BufferedReader reset() method
Selected Reading
Java - BufferedReader reset() methodDescriptionThe Java BufferedReader reset() method resets the stream to the most recent mark set by the mark(int readAheadLimit) method. If the mark() method was not called before reset(), or if the read limit specified in mark(int readAheadLimit) has been exceeded, calling reset() will throw an IOException. DeclarationFollowing is the declaration for java.io.BufferedReader.ready() method. public void reset() ParametersNA Return ValueThis method does not return any value. ExceptionIOException− If the stream is never been marked, or the mark has become invalid. Example - Using a BufferedReader reset() methodThe following example shows the usage of BufferedReader reset() method. BufferedReaderDemo.javapackage com.tutorialspoint;
import java.io.BufferedReader;
import java.io.StringReader;
public class BufferedReaderDemo {
public static void main(String[] args) throws Exception {
String s ="ABCDE";
StringReader sr = null;
BufferedReader br = null;
try {
sr = new StringReader(s);
// create new buffered reader
br = new BufferedReader(sr);
// reads and prints BufferedReader
System.out.println((char)br.read());
System.out.println((char)br.read());
// mark invoked at this position
br.mark(0);
System.out.println("mark() invoked");
System.out.println((char)br.read());
System.out.println((char)br.read());
// reset() repositioned the stream to the mark
br.reset();
System.out.println("reset() invoked");
System.out.println((char)br.read());
System.out.println((char)br.read());
} catch (Exception e) {
// exception occurred.
e.printStackTrace();
} finally {
// releases any system resources associated with the stream
if(sr!=null)
sr.close();
if(br!=null)
br.close();
}
}
}
OutputLet us compile and run the above program, this will produce the following result − A B mark() invoked C D reset() invoked C D Example - Resetting the Reader to Re-read ContentThe following example shows the usage of BufferedReader reset() method. BufferedReaderDemo.javapackage com.tutorialspoint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
public class BufferedReaderDemo {
public static void main(String[] args) {
String input = "Line 1: Java\nLine 2: BufferedReader\nLine 3: reset() Example";
// Initialize BufferedReader with a StringReader
try (BufferedReader reader = new BufferedReader(new StringReader(input))) {
// Read and print the first line
System.out.println("First read:");
System.out.println(reader.readLine());
// Mark the current position in the stream
reader.mark(100); // Allow up to 100 characters to be read before reset becomes invalid
// Read the next line
System.out.println("Second read:");
System.out.println(reader.readLine());
// Reset the reader to the marked position
reader.reset();
// Re-read the line after reset
System.out.println("Re-read after reset:");
System.out.println(reader.readLine());
} catch (IOException e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
}
OutputLet us compile and run the above program, this will produce the following result − First read: Line 1: Java Second read: Line 2: BufferedReader Re-read after reset: Line 2: BufferedReader Explanation
Example - Resetting the Reader to Read a Specific Portion of the Input AgainThe following example shows the usage of BufferedReader ready() method. BufferedReaderDemo.javapackage com.tutorialspoint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
public class BufferedReaderDemo {
public static void main(String[] args) {
String input = "1234567890";
// Initialize BufferedReader with a StringReader
try (BufferedReader reader = new BufferedReader(new StringReader(input))) {
char[] buffer = new char[5];
// Mark the starting position
reader.mark(10); // Allow up to 10 characters to be read before mark is invalid
// Read the first 5 characters
reader.read(buffer, 0, 5);
System.out.println("First read: " + new String(buffer));
// Reset to the marked position
reader.reset();
// Re-read the same 5 characters after reset
reader.read(buffer, 0, 5);
System.out.println("Re-read after reset: " + new String(buffer));
} catch (IOException e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
}
OutputLet us compile and run the above program, this will produce the following result − First read: 12345 Re-read after reset: 12345 Explanation
Key Points About reset()
These examples demonstrate how reset() works in conjunction with mark() to allow re-reading input efficiently. java_io_bufferedreader.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. |