StringReader reset() method in Java with Examples
The reset() method of StringReader Class in Java is used to reset the stream. After reset, if the stream has been marked, then this method attempts to reposition it at the mark, else it will try to position it to the starting.
Syntax:
public void reset()
Parameters: This method do not accepts any parameter.
Return Value: This method do not returns any value.
Exception: This method throws IOException:
- if some error occurs while input output
- if the stream has not been marked
- if the mark has been invalidated
- if the reset() method is not supported.
Below methods illustrates the working of reset() method:
Program 1:
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
try {
String str = "GeeksForGeeks" ;
StringReader reader
= new StringReader(str);
int ch;
for ( int i = 0 ; i < 10 ; i++) {
ch = reader.read();
System.out.print(( char )ch);
}
System.out.println();
reader.mark( 5 );
reader.reset();
for ( int i = 0 ; i < 5 ; i++) {
ch = reader.read();
System.out.print(( char )ch);
}
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Program 2:
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
try {
String str = "GeeksForGeeks" ;
StringReader reader
= new StringReader(str);
int ch;
for ( int i = 0 ; i < 10 ; i++) {
ch = reader.read();
System.out.print(( char )ch);
}
System.out.println();
reader.reset();
for ( int i = 0 ; i < 5 ; i++) {
ch = reader.read();
System.out.print(( char )ch);
}
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Reference: https://docs.oracle.com/javase/9/docs/api/java/io/StringReader.html#reset–
Last Updated :
13 Feb, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...