mark()
和方法如何reset()
准确地工作(在下面的代码中),一步一步?我尝试编写自己的示例,但开始抛出错误的标记异常或类似的异常,并且我无法理解在此代码中放置标记和重置方法的意义何在,因为我看不出有无区别。
import java.io.*;
class BufferedInputStreamDemo {
public static void main(String args[]) {
String s = "© is a copyright symbol, "
+ "however © isn't.\n";
byte buf[] = s.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(buf);
int c;
boolean marked = false;
//try_with_resources
try (BufferedInputStream f = new BufferedInputStream(in)) {
while ((c = f.read()) != -1) {
switch (c) {
case '&':
if (!marked) {
f.mark(32);
marked = true;
} else {
marked = false;
}
break;
case ';':
if (marked) {
marked = false;
System.out.print("(c)");
} else
System.out.print((char) c);
break;
case ' ':
if (marked) {
marked = false;
f.reset();
System.out.print("&");
} else
System.out.print((char) c);
break;
default:
if (!marked)
System.out.print((char) c);
break;
}
}
} catch (IOException e) {
System.out.println("I/O Error: " + e);
}
}
}