文件copy

复制目录文件函数

public static boolean copyFolder(File srcFile, File destFile) {
        if (!srcFile.isDirectory()) {
            return false;
        }
        if (!destFile.exists() && !destFile.mkdirs()) {
            return false;
        }
        boolean result = true;
        File[] list = srcFile.listFiles();
        if (list == null) {
            return result;
        }
        for (File f:list) {
            if (f.isDirectory()) {
                result &= copyFolder (f,new File(destFile,f.getName()));
            } else {
                result &= copyFile(f,new File(destFile,f.getName()));
            }
        }
        return result;
    }
    public static boolean copyFile(File srcFile, File destFile) {
        boolean result = false;
        try {
            InputStream in = new FileInputStream(srcFile);
            try {
                result = copyToFile(in, destFile);
            } finally  {
                in.close();
            }
        } catch (IOException e) {
            result = false;
        }
        return result;
    }
    public static boolean copyToFile(InputStream inputStream, File destFile) {
        try {
            if (destFile.exists()) {
                destFile.delete();
            }
            FileOutputStream out = new FileOutputStream(destFile);
            try {
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) >= 0) {
                    out.write(buffer, 0, bytesRead);
                }
            } finally {
                out.flush();
                try {
                    out.getFD().sync();
                } catch (IOException e) {
                }
                out.close();
            }
            return true;
        } catch (IOException e) {
            return false;
        }
    }


C


static void file_copy(const char *src, const char *des){
    FILE *in ,*out;
    char buf[4096];
    
    if ((in=fopen(src,"rb"))==NULL) {
        LOGE("Can't open %s\n", src);
        return;
    } 
    if((out=fopen(des,"wb"))==NULL){
        LOGE("Can't open %s\n", des);
        return;
    }
    while(fgets(buf, sizeof(buf), in)){
        fputs(buf, out);
    } 
    check_and_fclose(in, src);
    check_and_fclose(out, des);    
    
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值