输入流, 标记(), 复位()
方法和方法是如何准确工作的(在下面的代码中),一步一步?我试图编写自己的示例,但开始抛出错误的标记异常或类似情况,我无法理解在此代码中放置标记和重置方法的意义,因为我看不到与此或没有区别。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);
}
}
}