码迷,mamicode.com
首页 > 其他好文 > 详细

IO流复制文件

时间:2016-08-26 18:13:04      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

1. 经过测试可以复制 txt excel csv exe pdf文件其他格式的没测,估计也没问题

public class Hello {
    private static final  String LINE_SEPARATOR = System.getProperty("line.separator");//换行
    public static void main(String[] args) throws Exception {
        FileInputStream reader = new FileInputStream("d:/毕向东Java基础课堂笔记.pdf");
        FileOutputStream writer = new FileOutputStream("e:\\毕向东Java基础课堂笔记.pdf");
        int len = 0;
        byte[] buffer = new byte[1024*1024];//1MB缓存
        while((len = reader.read(buffer))!=-1){ //len = reader.read(buffer) buffer千万不要忘了,否则复制的文件超大
            writer.write(buffer ,0 , len);
            writer.flush();
        }
        System.out.println("复制成功");
        writer.close();
        reader.close();
    }
}

 2.优化复制文件

    public static void main(String[] args) throws Exception {
        File src = new File("d:/Evernote_6.1.2.2292.exe");
        File des = new File("e:\\NEW_Evernote_6.1.2.2292.exe");

        copyFile(src,des);
    }

    private static void copyFile(File src, File des) throws  Exception{
        FileInputStream reader = new FileInputStream(src);
        FileOutputStream writer = new FileOutputStream(des);
        int len = 0;
        byte[] buffer = new byte[1024*1024];//1MB缓存
        while((len = reader.read(buffer))!=-1){ //len = reader.read(buffer) buffer千万不要忘了,否则复制的文件超大,陷入死循环
            writer.write(buffer ,0 , len);
            writer.flush();
        }
        System.out.println("复制成功");
        writer.close();
        reader.close();
    }

 

IO流复制文件

标签:

原文地址:http://www.cnblogs.com/bravolove/p/5811307.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!