InputStream中的三個方法
1.mark(int readlimit)
在流的readlimit處標記。
2.reset()
從被標記的地方開始讀。
3.markSupported()
判斷是否支持標記。
二次復用
public class FileReset {
? ? public static void main(String args[]) throws IOException {
? ? ? ? //一般要配合mark()方法使用。
? ? ? ? String content = "BoyceZhang!";
? ? ? ? InputStream inputStream = new ByteArrayInputStream(content.getBytes());
// 判斷該輸入流是否支持mark操作
? ? ? ? if (!inputStream.markSupported()) {
? ? ? ? ? ? System.out.println("mark/reset not supported!");
? ? ? ? }
? ? ? ? int ch;
? ? ? ? boolean marked = false;
? ? ? ? while ((ch = inputStream.read()) != -1) {
? ? ? ? ? ? //讀取一個字符輸出一個字符
? ? ? ? ? ? System.out.print((char) ch);
? ? ? ? ? ? //讀到 'e'的時候標記一下
? ? ? ? ? ? if (((char) ch == 'Z') & !marked) {
? ? ? ? ? ? ? ? inputStream.mark(content.length());? //先不要理會mark的參數(shù)
? ? ? ? ? ? ? ? marked = true;
? ? ? ? ? ? }
? ? ? ? ? ? //讀到'!'的時候重新回到標記位置開始讀
? ? ? ? ? ? if ((char) ch == '!' && marked) {
? ? ? ? ? ? ? ? inputStream.reset();
? ? ? ? ? ? ? ? marked = false;
? ? ? ? ? ? }
}
}
//程序最終輸出:BoyceZhang!Zhang!