import java.io.*;
class CopybufDemo
{
public static void main(String[] args) //throws IOException
{
copy_2();
}
public static void copy_2()// throws IOException
{
BufferedWriter bufw =null;
BufferedReader bufr =null;
try
{
bufw=new BufferedWriter(new FileWriter("e:\\bufWriter_Copy_new.txt"));
bufr=new BufferedReader(new FileReader("c:\\bufWriter_Copy.txt"));
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)
try
{
bufr.close();
}
catch (IOException e)
{
throw new RuntimeException("写入数据失败");
}
if(bufw!=null)
try
{
bufw.close();
}
catch (IOException e)
{
throw new RuntimeException("读取数据失败");
}
}
}
} |
|