- import java.io.*;
- class FileTest
- {
- public static void main(String[] args) //throws IOException
- {
- copy_2();
- }
- public static void copy_2()
- {
- FileReader fr = null;
- FileWriter fw = null;
- try
- {
- fr = new FileReader("demo.txt");
- fw = new FileWriter("democopy_2.txt");
- int len = 0;
- char[] buf = new char[1024];//定义缓冲区
- while ((len = fr.read(buf))!=-1)//读入缓冲区
- {
- fw.write(buf,0,len);//这里又写入缓冲区,
- //不怕这次写入还没刷新到硬盘,就被下一次
- //fr.read(buf)读入覆盖了吗?
- }
- }
- catch (IOException e)
- {
- throw new RuntimeException("读写失败");
- }
- finally
- {
- if (fr!=null)
- {
- try
- {
- fr.close();
- }
- catch (IOException e)
- {
- }
-
- }
- if (fw!=null)
- {
- try
- {
- fw.close();
- }
- catch (IOException e)
- {
- }
-
- }
- }
- }
- }
复制代码 while ((len = fr.read(buf))!=-1)//读入缓冲区
{
fw.write(buf,0,len);//这里又写入缓冲区,
//不怕这次写入还没刷新到硬盘,就被下一次
//fr.read(buf)读入覆盖了吗?
|