写的不是很好,希望大家多多指正
BufferedReader常用的方法:readLine()
LineNumberReader常用的方法:getLineNumber(),setLineNumber(int lineNumber)
public class IOBufferedReader {
public static void main(String[] args) {
char[] c = new String("Hello\nWorld").toCharArray();
char[] c1 = new String("Hello\nWorld\nHello\nWorld").toCharArray();
Reader car = new CharArrayReader(c);
Reader car1 = new CharArrayReader(c1);
BufferedReader br = new BufferedReader(car);
LineNumberReader lnr = new LineNumberReader(car1);
try {
System.out.println("BufferedReader-----" + br.readLine());
lnr.mark(3);
System.out.println(lnr.getLineNumber() + ":" + lnr.readLine());
System.out.println(lnr.getLineNumber() + ":" + lnr.readLine());
lnr.reset();
lnr.setLineNumber(10);
System.out.println("重设行号之后\n" + lnr.getLineNumber() + ":" + lnr.readLine());
System.out.println( lnr.getLineNumber() + ":" + lnr.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
}
控制台结果:
BufferedReader-----Hello
0:Hello
1:World
重设行号之后
10:Hello
11:World
BufferedReader的常用获取方法:
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("test.txt")));
BufferedReader br = new BufferedReader( new FileReader(new File ("test.txt")) );