快速复制大文件、多个文件

之前写过一个copy文件的方法,30多M要复制1分多钟.......想死,现在忘记了那个方法。只有下面这个方法

 

快速复制大文件、多个文件(包含文件夹):

public static void copyFile(File file, String dest) {
        String pathWindows = file.getAbsolutePath() + File.separator;
        String[] list = file.list();
        for (String name : list) {
            copy(pathWindows + name, dest + File.separator + name);
        }
    }

    private static void copy(String src, String dest) {
        BufferedInputStream reader = null;
        BufferedOutputStream writer = null;
        try {
            File file = new File(src);
            if (file.isDirectory()) {
                File fileDir = new File(dest);
                String tempStr = null;
                int i = 0;
                // 存在则重会命名
                while (fileDir.exists()) {
                    i++;
                    tempStr = dest + "(" + i + ")";
                    fileDir = new File(tempStr);
                }
                if (fileDir.mkdirs())
                    copyFile(file, fileDir.getAbsolutePath());

            } else {
                reader = new BufferedInputStream(new FileInputStream(file));
                writer = new BufferedOutputStream(new FileOutputStream(dest));
                byte[] buff = new byte[reader.available()];
                while ((reader.read(buff)) != -1) {
                    writer.write(buff);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.flush();
                    writer.close();
                }
                if (reader != null)
                    reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

 

测试类:

public class Test {
    public static void main(String[] args) {
        // findFile("J:\\lhc");
        copyFile();
    }

public static void copyFile() {

        File file = new File("D:\\temp\\Camera");
        String pathWindows = file.getAbsolutePath() + "\\";
        String[] list = file.list();
        long millisRead = System.currentTimeMillis();
        for (String name : list) {
            copy(pathWindows + name, "D:\\temp\\新建文件夹\\" + name);
        }
        System.out.println("time = "
                + (System.currentTimeMillis() - millisRead) + "ms; 共:"
                + list.length + "个文件");
    }

    public static void copyFile(File file, String dest) {
        String pathWindows = file.getAbsolutePath() + File.separator;
        String[] list = file.list();
        for (String name : list) {
            copy(pathWindows + name, dest + File.separator + name);
        }
    }

    private static void copy(String src, String dest) {
        BufferedInputStream reader = null;
        BufferedOutputStream writer = null;
        try {
            File file = new File(src);
            if (file.isDirectory()) {
                File fileDir = new File(dest);
                String tempStr = null;
                int i = 0;
                // 存在则重会命名
                while (fileDir.exists()) {
                    i++;
                    tempStr = dest + "(" + i + ")";
                    fileDir = new File(tempStr);
                }
                if (fileDir.mkdirs())
                    copyFile(file, fileDir.getAbsolutePath());

            } else {
                reader = new BufferedInputStream(new FileInputStream(file));
                writer = new BufferedOutputStream(new FileOutputStream(dest));
                byte[] buff = new byte[reader.available()];
                while ((reader.read(buff)) != -1) {
                    writer.write(buff);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.flush();
                    writer.close();
                }
                if (reader != null)
                    reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

 

 

 

如果您在编写java过程中有出现问题,如果不嫌弃小编我学历浅薄,欢迎添加我的微信一共讨论,感谢您的支持!微信号:atgeretg


觉得还可以请您打赏,您的十分满意是小编的无限动力。

 

 

FastCopy ver2.08 2011/02/28 SHIROUZU Hiroaki FastCopy is the Fastest Copy/Delete Software on Windows. It can copy/delete unicode and over MAX_PATH(260byte) pathname files. It always run by multi-threading. It don't use MFC, it is compact and don't requre mfcxx.dll. FastCopy is BSD license, you can modify and use. License: --------------------------------------------------------------- Copyright 2004-2011 SHIROUZU Hiroaki All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY SHIROUZU Hiroaki ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SHIROUZU Hiroaki OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --------------------------------------------------------------- Usage丗 Please see fastcopy.chm Build: FastCopy/Install/ShellExt ... VC4.1 ShellExt64 ... x64 compiler
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值