通过缓冲区复制一个.java文件
readLine方法返回的时候只返回回车符之前的数据内容,并不返回回车符。
import java.io.*;
class CopyTextByBuf
{
public static void main(String[] args)
{
BufferedReader bufr = null;
BufferedWriter bufw = null;
try
{
bufr = new BufferedReader(new FileReader("MapDemo2.java"));
bufw = new BufferedWriter(new FileWriter("CopyMapDemo2.java"));
String line = null;
while((line=bufr.readLine())!=null)
{
bufw.write(line);
bufw.newLine();
bufw.flush();
}
}
catch (IOException e)
{
throw new RuntimeException("读写失败");
}
finally
{
try
{
if(bufr!=null)
{
bufr.close();
}
}
catch (IOException e)
{
throw new RuntimeException("读取关闭失败");
}
try
{
if(bufw!=null)
{
bufw.close();
}
}
catch (IOException e)
{
throw new RuntimeException("写入关闭失败");
}
}
}
}
readLine方法的原理,无论是读一行,读取多个字符。其实最终都是在硬盘上一个个读取。所以最终使用的还是read方法一次读一个的方法。
只要有\n的就是一行数据,把回车符前数据临时存储起来,到回车符就把数据一次全返回来。
image.png
网友评论