下面列出了java.io.LineNumberReader#mark ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private boolean gobbleTo(LineNumberReader reader, String target, boolean throwException) throws IOException, ParseException {
String line = "";
reader.mark(255);
while (line.isEmpty()) {
line = reader.readLine();
if (line == null)
return false;
}
if (!line.trim().equals(target)) {
if (throwException)
throw new ParseException(target + " not found.", reader.getLineNumber());
else {
reader.reset();
return false;
}
}
return true;
}
public void test_markI() throws IOException {
lnr = new LineNumberReader(new StringReader(text));
String line;
lnr.skip(80);
lnr.mark(100);
line = lnr.readLine();
lnr.reset();
assertTrue("Test 1: Failed to return to marked position.",
line.equals(lnr.readLine()));
lnr.close();
try {
lnr.mark(42);
fail("Test 2: IOException expected.");
} catch (IOException e) {
// Expected.
}
// The spec does not say the mark has to be invalidated
}
public void test_reset() throws IOException {
lnr = new LineNumberReader(new StringReader(text));
assertEquals("Test 1: Returned incorrect line number;",
0, lnr.getLineNumber());
String line = null;
lnr.mark(100);
lnr.readLine();
lnr.reset();
line = lnr.readLine();
assertEquals("Test 2: Failed to reset reader", "0", line);
lnr.mark(100);
lnr.close();
try {
lnr.reset();
fail("Test 3: IOException expected.");
} catch (IOException e) {
// Expected.
}
}