例子
以下示例显示java.io.LineNumberInputStream.mark(int readlimit)方法的用法。
package com.jc2182;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;
public class LineNumberInputStreamDemo {
public static void main(String[] args) throws IOException {
LineNumberInputStream lnis = null;
FileInputStream fis = null;
int i,j;
char c;
try {
// create new input streams
fis = new FileInputStream("C:/test.txt");
lnis = new LineNumberInputStream(fis);
// reads and prints data from input stream
System.out.println((char)lnis.read());
System.out.println((char)lnis.read());
// mark invoked at this position
lnis.mark(0);
System.out.println("mark() invoked");
System.out.println((char)lnis.read());
System.out.println((char)lnis.read());
// if this input stream supports mark() an reset()
if(lnis.markSupported()) {
// reset() repositioned the stream to the mark
lnis.reset();
System.out.println("reset() invoked");
System.out.println((char)lnis.read());
System.out.println((char)lnis.read());
}
} catch(Exception e) {
// if any error occurs
e.printStackTrace();
} finally {
// closes the stream and releases any system resources
if(fis!=null)
fis.close();
if(lnis!=null)
lnis.close();
}
}
}
假设我们有一个文本文件c:/test.txt ,其内容如下。该文件将用作示例程序的输入-
让我们编译并运行以上程序,这将产生以下结果-