java中实现将一个文件中的多个文件通过filechannel多次复制

java中实现将一个文件中的多个文件通过filechannel多次复制

package cn.spdb.day06.code;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class test3 {
final static String SOURCESTRING = “D:\file”;
final static String OUTPUTSTRING = “D:\file1”;

public static void main(String[] args) throws IOException {
    long start = System.currentTimeMillis();
    // 将SOURCESTRING下的文件复制3次到OUTPUTSTRING目录下
    LCopyFileUtils nn=new LCopyFileUtils();
    nn.copyFile(SOURCESTRING, OUTPUTSTRING, 3000);
    long end = System.currentTimeMillis();
    System.out.println("复制文件成功");
    System.out.println("耗时:" + (end - start)  + " s");
}
/**
 *
 * 参考:
 * Java将一个目录下的所有数据复制到另一个目录下:https://www.jb51.net/article/167726.htm
 * Java复制文件的4种方式:https://www.jb51.net/article/70412.htm
 * */

static class LCopyFileUtils {

    /**
     * 复制srcPath路径下的文件到destPath目录下
     *
     * @param srcPath 源文件路径
     * @param destPath 输出路径
     * @param count 每个文件的复制次数
     * @return 是否复制成功
     */
    public  boolean copyFile(String srcPath, String destPath, int count) throws IOException {
        File fileSrc = new File(srcPath);
        File[] files = fileSrc.listFiles();
        if (files == null) {
            System.out.println("Error:源文件夹下没有文件");
            return false;
        }
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                File file = null;
                String fileName = files[i].getName();

                String filePrefix = fileName.substring(0, fileName.lastIndexOf("."));
                String fileSuffix = fileName.substring(fileName.lastIndexOf("."));

                // 每个文件复制Count次
                for (int j = 0; j < count; j++) {
                    file = new File(destPath + File.separator + filePrefix + "_" + i + "_" + j + fileSuffix);// 创建文件
                    copyFileUsingFileChannels(files[i], file);
                }
            }
        }
        return true;
    }

    /**
     * 复制文件srcFile到destFile
     *
     * @param srcFile 源文件
     * @param destFile 目的文件
     */
    public  void copyFileUsingFileChannels(File srcFile, File destFile) throws IOException {

        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {
            inputChannel = new FileInputStream(srcFile).getChannel();
            outputChannel = new FileOutputStream(destFile).getChannel();
            outputChannel.transferFrom(inputChannel, 0, inputChannel.size());

// System.out.println(“复制文件成功:” + srcFile.getName() + " -> " + destFile.getName());
}
catch (Exception e) {
// System.out.println(“Error:复制文件失败:” + srcFile.getName() + " -> " + destFile.getName());
}
finally {
if (inputChannel != null) {
inputChannel.close();
}
if (outputChannel != null) {
outputChannel.close();
}
}

    }

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值