吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1682|回复: 7
收起左侧

[其他转载] 高效拷贝文件和文件夹

[复制链接]
roberttoday 发表于 2020-11-11 15:22
1、使用channel高效拷贝文件:
[Java] 纯文本查看 复制代码
public static void copyFileUsingFileChannels(File source, File dest)
        throws IOException {
    FileChannel inputChannel = null;
    FileChannel outputChannel = null;
    try {
        inputChannel = new FileInputStream(source).getChannel();
        outputChannel = new FileOutputStream(dest).getChannel();
        outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
        Log.e("ch_test", "---copy file success >>> " + dest.getAbsolutePath());
    } finally {
        inputChannel.close();
        outputChannel.close();
    }
}


2、拷贝文件夹及其子文件:(递归思想)
[Java] 纯文本查看 复制代码
public static boolean copyFolder(String oldPath, String newPath) {
    try {
        File newFile = new File(newPath);
        if (!newFile.exists()) {
            if (!newFile.mkdirs()) {
                Log.e("--Method--", "copyFolder: cannot create directory.");
                return false;
            }
        }
        File oldFile = new File(oldPath);
        String[] files = oldFile.list();
        File temp;
        for (String file : files) {
            if (oldPath.endsWith(File.separator)) {
                temp = new File(oldPath + file);
            } else {
                temp = new File(oldPath + File.separator + file);
            }

            if (temp.isDirectory()) {   //如果是子文件夹
                copyFolder(oldPath + "/" + file, newPath + "/" + file);
            } else if (!temp.exists()) {
                Log.e("--Method--", "copyFolder:  oldFile not exist.");
                return false;
            } else if (!temp.isFile()) {
                Log.e("--Method--", "copyFolder:  oldFile not file.");
                return false;
            } else if (!temp.canRead()) {
                Log.e("--Method--", "copyFolder:  oldFile cannot read.");
                return false;
            } else {
                FileInputStream fileInputStream = new FileInputStream(temp);
                FileOutputStream fileOutputStream = new FileOutputStream(newPath + "/" + temp.getName());
                byte[] buffer = new byte[1024];
                int byteRead;
                while ((byteRead = fileInputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, byteRead);
                }
                fileInputStream.close();
                fileOutputStream.flush();
                fileOutputStream.close();
            }

            /* 如果不需要打log,可以使用下面的语句
            if (temp.isDirectory()) {   //如果是子文件夹
                copyFolder(oldPath + "/" + file, newPath + "/" + file);
            } else if (temp.exists() && temp.isFile() && temp.canRead()) {
                FileInputStream fileInputStream = new FileInputStream(temp);
                FileOutputStream fileOutputStream = new FileOutputStream(newPath + "/" + temp.getName());
                byte[] buffer = new byte[1024];
                int byteRead;
                while ((byteRead = fileInputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, byteRead);
                }
                fileInputStream.close();
                fileOutputStream.flush();
                fileOutputStream.close();
            }
             */
        }
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}


发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

yizems 发表于 2020-11-11 16:01
和 okio 比如何? 还是说这个只是 api 上的简单呢
红蓝黄 发表于 2020-11-11 16:17
鸭子咯咯哒~ 发表于 2020-11-11 16:33
 楼主| roberttoday 发表于 2020-11-13 14:15
yizems 发表于 2020-11-11 16:01
和 okio 比如何? 还是说这个只是 api 上的简单呢

具体没测试,okio也是基于java io/nio。感觉看具体场景吧,和内存有关系吧。
 楼主| roberttoday 发表于 2020-11-13 14:16

尽管绕过了cpu申请,还是需要看拷贝文件的大小及数量,以及当时机器的内存性能。
 楼主| roberttoday 发表于 2020-11-13 14:18
jiushu52 发表于 2020-11-11 16:04
不明觉厉,高效的传输这个是自己做一个代码?一个程序?

对,自己在java平台做一个。如果能够在c++层面实现应该效率会更高一些。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-3 06:50

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表