/*
* 缓冲区的出现是为了提高流的操作效率
* 所以在创建缓冲区之前必须要现有流对象,
* */
BufferedReader的子类LineNumberReader
* 缓冲区的出现是为了提高流的操作效率
* 所以在创建缓冲区之前必须要现有流对象,
* */
public class CopyTextByBuf {
public static void main(String[] args) throws Exception {
BufferedReader bufr = null;
BufferedWriter bufw = null;
bufr = new BufferedReader(new FileReader("BufferedWriterDemo.java"));
bufw = new BufferedWriter(new FileWriter("bufWriter_Copy.txt"));
String line = null;
//readLine()一次读一行,但只返回回车符之前的字符,不包含回车符,读完时返回null
while((line = bufr.readLine())!=null)
{
//将读取到的字符串line写入到缓冲区中
bufw.write(line);
//写入一个行分隔符
bufw.newLine();
//刷新该流的缓冲
bufw.flush();
}
}
}
BufferedReader的子类LineNumberReader
public class LineNumberReaderDemo {
public static void main(String[] args) throws Exception {
FileReader fr = new FileReader("BufferedWriterDemo.java");
LineNumberReader lnr = new LineNumberReader(fr);
String line = null;
lnr.setLineNumber(100);
while((line = lnr.readLine())!=null)
{
//获得当前行号
System.out.println(lnr.getLineNumber()+"--"+line);
}
lnr.close();
}
}