java_多线程复制文件夹(拷贝目录):真.多线程(为每个文件分配一个复制线程)

package experiment10.exp1;

import experiment9.FileName;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author xuchaoxin
 * @Date 12/19/2020 9:41 AM
 * @Version 1.0
 */
public class CreatCopyPathThreads {
    static int count = 0;
    static List<Thread> list = new ArrayList<>();//存储线程对象.
    /*本递归复制函数具有检查并创建新目录的能力,*/

    /**
     * @param srcPath 可能是个目录,也可能是个文件
     * @param desPath 当srcPath 是一个目录的时候,desPath也必须是一个目录(1.约定该参数和srcPath是相同尾缀时;2.不用关心尾缀
     *                可能的参数组合包括:dir->dir;file->file;file->dir;(不可能是dir->file)
     * @throws IOException
     * @throws InterruptedException
     */
    static void createCopyPathThreads(String srcPath, String desPath) throws IOException, InterruptedException {
        // System.out.println(count++);//testing(通过中途的内部打印来监视count的累计情况.)
        File srcPathFile = new File(srcPath);
        File desPathFile = new File(desPath);
        /* 如果源目标是个目录,则要保证目标目录存在: */
//        if (srcPathFile.isDirectory()&&!(new File(desPath)).exists()) {
//            (new File(desPath)).mkdir();//创建目录
//        }
        /*分析源路径*/
        if (srcPathFile.isDirectory()) {
            //确保对应的目标目录存在
            if (!(new File(desPath)).exists()) {
                desPathFile.mkdir();//创建目录
            }
            /*遍历当前目录下的子目录和文件(即各个条目)*/
            String[] filesPathString = srcPathFile.list();
            for (String subItem : filesPathString) {

                String absoluteSrcSubItemStr = srcPath + File.separator + subItem;
                String absoluteDesSubItemStr = desPath + File.separator + subItem;
//
//                File subItemFile = new File(absoluteSrcSubItemStr);
//                File desSubItemFile = new File(absoluteDesSubItemStr);
                /*直接递归:*/
                createCopyPathThreads(absoluteSrcSubItemStr, absoluteDesSubItemStr);

            }//endFor
        }//end if
        else {
            Thread thread = new Thread(() -> {
                if (!desPathFile.exists()) {
                    try {
                        //复制file
                        Files.copy(srcPathFile.toPath(), desPathFile.toPath());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
            thread.setName("threadNo" + count++);
            list.add(thread);
        }
        //System.out.println("The replication operation has been successfully executed!");
    }//endCopyFunction

    public static int getCount() {
        return count = list.size();
    }

    /**
     * //todo  相应而生的策略:先创建所有需要创建的进程,然后统一start;就解决了线程数的统计问题(统一join,就解决了时间统计的问题)(而且不会退化为单线程排队执行.)可以发现,进程的创建和执行不是绑在一起的,而是通过start方法才会启动.可见其用处(灵活)
     * @param srcPath
     * @param desPath
     * @throws InterruptedException
     */
    public static void copyPath(String srcPath, String desPath) throws InterruptedException, IOException {
        createCopyPathThreads(srcPath, desPath);
        for (Thread thread : list) {
            thread.start();
        }
        for (Thread thread : list) {
            thread.join();
        }
    }

    //test the class
    public static void main(String[] args) throws IOException, InterruptedException {
        //计时开始.
        long startTime = System.currentTimeMillis();
        
//一句话,实现一个需求:
        copyPath(FileName.fileName11_1, "D:/src");
        System.out.println("共启用了" + CreatCopyPathThreads.getCount() + "个线程");
        System.out.println("目录拷贝完成,共耗时" + (System.currentTimeMillis() - startTime) + "毫秒");
    }
}//end class

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 以下是Java多线程拷贝文件的示例代码: ```java import java.io.*; public class MultiThreadFileCopy { public static void main(String[] args) { String srcFile = "source_file_path"; String destFile = "destination_file_path"; int threadNum = 5; File src = new File(srcFile); long startTime = System.currentTimeMillis(); if (src.exists()) { long fileSize = src.length(); long blockSize = fileSize / threadNum; for (int i = 0; i < threadNum; i++) { long startIndex = i * blockSize; long endIndex = (i == threadNum - 1) ? fileSize - 1 : (i + 1) * blockSize - 1; new CopyThread(srcFile, destFile, startIndex, endIndex).start(); } } long endTime = System.currentTimeMillis(); System.out.println("Total time taken: " + (endTime - startTime) + "ms"); } static class CopyThread extends Thread { private String srcFile; private String destFile; private long startIndex; private long endIndex; public CopyThread(String srcFile, String destFile, long startIndex, long endIndex) { this.srcFile = srcFile; this.destFile = destFile; this.startIndex = startIndex; this.endIndex = endIndex; } @Override public void run() { try (RandomAccessFile src = new RandomAccessFile(srcFile, "r"); RandomAccessFile dest = new RandomAccessFile(destFile, "rw")) { src.seek(startIndex); dest.seek(startIndex); byte[] buffer = new byte[1024]; int length; while ((length = src.read(buffer)) > 0) { if (startIndex + length > endIndex) { length = (int) (endIndex - startIndex + 1); } dest.write(buffer, 0, length); startIndex += length; if (startIndex > endIndex) { break; } } System.out.println(Thread.currentThread().getName() + " finished copying."); } catch (IOException e) { e.printStackTrace(); } } } } ``` 该示例代码首先将源文件路径、目标文件路径和线程数作为参数传入,然后计算出每个线程需要拷贝文件块的起始位置和结束位置。接下来,该示例代码启动多个线程拷贝文件块。每个线程使用RandomAccessFile类来读取源文件和写入目标文件。在拷贝过程每个线程都会更新自己的起始位置,并在拷贝完成后打印出自己的线程名称。最后,该示例代码计算并输出整个拷贝操作的总耗时。 ### 回答2: 在Java,可以使用多线程进行文件拷贝操作。以下是一个简单的示例代码,用于演示如何使用多线程拷贝文件。 ```java import java.io.*; public class FileCopyWithThreads { public static void main(String[] args) { String sourcePath = "C:\\path\\to\\source\\file.txt"; String destPath = "C:\\path\\to\\destination\\file.txt"; int numberOfThreads = 4; try { // 创建输入流和输出流 FileInputStream fis = new FileInputStream(sourcePath); FileOutputStream fos = new FileOutputStream(destPath); // 获取源文件的大小 File sourceFile = new File(sourcePath); long fileSize = sourceFile.length(); // 计算每个线程需要拷贝的字节数 long chunkSize = fileSize / numberOfThreads; // 创建线程数组 CopyThread[] threads = new CopyThread[numberOfThreads]; // 启动线程 for (int i = 0; i < numberOfThreads; i++) { long startPos = i * chunkSize; long endPos = (i == numberOfThreads - 1) ? fileSize - 1 : (i + 1) * chunkSize - 1; threads[i] = new CopyThread(fis, fos, startPos, endPos); threads[i].start(); } // 等待所有线程完成 for (int i = 0; i < numberOfThreads; i++) { threads[i].join(); } // 关闭输入流和输出流 fis.close(); fos.close(); System.out.println("文件拷贝完成!"); } catch (Exception e) { e.printStackTrace(); } } public static class CopyThread extends Thread { private FileInputStream fis; private FileOutputStream fos; private long startPos; private long endPos; public CopyThread(FileInputStream fis, FileOutputStream fos, long startPos, long endPos) { this.fis = fis; this.fos = fos; this.startPos = startPos; this.endPos = endPos; } @Override public void run() { try { // 设置输入流的起始位置 fis.getChannel().position(startPos); // 定义缓冲区 byte[] buffer = new byte[1024]; int bytesRead; // 从输入流读取数据,写入输出流 while ((bytesRead = fis.read(buffer)) != -1) { fos.write(buffer, 0, bytesRead); // 检查是否超过了该线程的结束位置 if (fis.getChannel().position() > endPos) { break; } } } catch (Exception e) { e.printStackTrace(); } } } } ``` 在上述示例,我们通过将文件分割成多个块,并为每个线程分配一个块来实现多线程拷贝文件每个线程负责读取指定块的数据,并将数据写入目标文件。最后,等待所有线程完成后,关闭输入流和输出流,完成文件拷贝操作。 注意:在实际应用,需要根据具体情况对代码进行适当的修改,并添加必要的异常处理和错误检查。另外,文件拷贝过程可能涉及到文件的锁定和同步问题,需要根据具体需求采取适当的措施来解决。 ### 回答3: 在Java实现多线程拷贝文件可以通过使用线程池和线程等待的方式来实现。 首先,我们需要创建一个FixedThreadPool线程池,用于控制并发执行的线程数量,并且创建一个计数器来追踪文件复制的进度。然后,我们可以遍历要拷贝文件列表,为每个文件创建一个任务,并将其提交给线程池执行。 文件拷贝的任务可以定义为一个Runnable接口的实现类,其包含拷贝文件的逻辑。在拷贝的过程,我们可以使用FileChannel来读取源文件,并用FileChannel来写入新文件,以提高拷贝的效率。每个任务执行完毕后,计数器加一,表示一个文件已经完成拷贝。 在所有的文件拷贝任务提交给线程池之后,我们可以调用线程池的awaitTermination方法,等待所有任务执行完毕,然后关闭线程池。同时,可以将计数器的值与文件列表的长度进行比较,如果相等则表示所有文件都已经拷贝完成。 在整个过程,使用多线程可以充分利用CPU资源并提高文件拷贝的速度。然而,需要注意的是在进行文件拷贝的时候要确保对共享资源的正确访问,避免出现并发问题,常见的方法是使用同步锁或者使用线程安全的类。 总的来说,通过使用多线程拷贝文件可以有效提高文件拷贝的效率,在大量文件需要拷贝的情况下尤为适用。同时,我们还应该注意对线程安全的处理,保证拷贝过程的正确性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值