根據文檔,void mark(int readlimit):標記此輸入流中的當前位置。PushbackInputStream 的 mark 方法不執行任何操作。void reset():將此流重新定位到上次在此輸入流上調用 mark 方法時的位置。PushbackInputStream 類的重置方法除了拋出 IOException 之外什么也不做。您可以檢查上面的“不執行任何操作”。那么,如果是這種情況,為什么以及在哪里有用?在什么情況下我可以使用以上兩種方法?下面是示例:import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.PrintWriter; import java.io.PushbackInputStream; public class PushbackInputStreamDemo { public static void main(String arg[]) throws Exception { PrintWriter pw = new PrintWriter(System.out, true); String str = "GeeksforGeeks a computer science portal "; byte b[] = str.getBytes(); ByteArrayInputStream bout = new ByteArrayInputStream(b); PushbackInputStream push = new PushbackInputStream(bout); int c; while((c=push.read())!=-1) { pw.print((char)c); } pw.println(); // marking the position push.mark(5); // reseting is not supported throw exception push.reset(); pw.close(); } } 上面是示例,但沒有得到這兩種方法的確切作用。請指導。
1 回答

猛跑小豬
TA貢獻1858條經驗 獲得超8個贊
和方法是可選操作,并非每個 InputStreammark
都reset
需要支持。你可以打電話markSupported
詢問是否可以。
PushbackInputStream 不支持這些方法。
這些方法仍然存在,因為它們是在InputStream
接口中定義的。也許是一個糟糕的設計決策(可能已添加到單獨的界面中),但事實就是如此。
添加回答
舉報
0/150
提交
取消