Java基础_IO_文件复制操作

//首先确定你要复制的文件及其路径

//其次确定你要复制到什么路径下及其文件名

//导入程序所要用到的包

//import java.io.FileReader;
//import java.io.FileWriter;

import java.io.FileReader;
import java.io.FileWriter;

public class CopyTest
{
    public static void main(String[] args)
    {
        copy();
    }

    public static void copy()
    {
        FileReader fr = null;
        FileWriter fw = null;
        try
        {
            fr = new FileReader("E:\\Heima code\\test.txt");
            fw = new FileWriter("E:\\Heima code\\copy.txt");
            char[] buf = new char[1024];
            int len = 0;
            while ((len = fr.read(buf)) != -1)
            {
                fw.write(buf, 0, len);
            }
        } catch (Exception e)
        {
            throw new RuntimeException("读写失败");
        } finally
        {
            try
            {
                if (fr != null)
                {
                    fr.close();
                }
            } catch (Exception e)
            {
            } finally
            {
                try
                {
                    if (fw != null)
                    {
                        fw.close();
                    }
                } catch (Exception e2)
                {
                    e2.printStackTrace();
                }
            }
        }
    }
}

 

posted @ 2014-07-09 14:06  loneliness__白色  阅读(77)  评论(0)    收藏  举报