using the mark () method of the BufferedReader class of the Igamo package, I encountered a strange situation, that is, sometimes I filled in 0 in mark (0), and some of the code behaved normally and some did not. The code is as follows:
import java.io.*;
class test {
public static void main(String[] args) throws IOException {
try(
BufferedReader r = new BufferedReader(new StringReader(
"Happy Birthday to You!\n" +
"Happy Birthday, dear " + System.getProperty("user.name") + "!"));)
{
if(r.markSupported()) {
r.mark(0); // save the data we are about to read
System.out.println(r.readLine()); // read the first line
r.reset(); // jump back to the marked position
//r.mark(1000); // start saving the data again
System.out.println(r.readLine()); // read the first line again
System.out.println(r.readLine()); // read the second line
r.reset(); // jump back to the marked position
System.out.println(r.readLine()); // read the first line one final
}
}catch (IOException e){
System.out.println(e);
}
}
}
run
Happy Birthday to Youyou!
java.io.IOException: Mark invalid
import java.io.*;
import static java.lang.System.out;
public class App {
public static final String TEST_STR = "Line 111111111111111111\nLine 222222222222222222\nLine 33333333333333333333333\nLine 4444444444444444444444\n";
public static void main(String[] args) {
try (BufferedReader in = new BufferedReader(new StringReader(TEST_STR))) {
// first check if this Reader support mark operation
if (in.markSupported()) {
out.println(in.readLine());
in.mark(0); // mark "Line 2"
out.println(in.readLine());
out.println(in.readLine());
in.reset(); // reset "Line 2"
out.println(in.readLine());
in.reset(); // reset "Line 2"
out.println(in.readLine());
in.mark(0); // mark "Line 3"
out.println(in.readLine());
in.reset(); // reset "Line 3"
out.println(in.readLine());
out.println(in.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
there is no problem running.
Line 111111111111111111
Line 22222222222222
Line 333333333333333333333
Line 2222222222222222
Line 2222222222222222
Line 3333333333333333
Line 3333333333333333
Line 4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444333333333333333333
runs on intellij idea.
Please point out that there is something wrong with that place.