mark()
和reset()
方法是如何一步一步準確工作的(在下面的代碼中)?我試圖寫我自己的例子,但開始拋出錯誤的標記異常或類似的,我不明白什麼是在這個代碼中放置標記和重置方法,因爲我沒有看到與這個或沒有差異。InputStream,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);
}
}
}
拋出什麼異常?你有沒有堆棧跟蹤?請更具體地談談你的問題。 – Lion
拋出的異常是'I/O錯誤:java.io.IOException:重置爲無效標記',但我寫了另一個示例中拋出異常。主要是我問什麼是標記(32)並重置**在這個例子**中。 – ashur
@ user1165499查看我的答案,瞭解有關此代碼如何工作的細節,以及我懷疑存在的問題,在另一個示例中。 – dan