欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Java多线程实现复制文件

时间:2023-03-20 10:27:30|栏目:JAVA代码|点击:

本文实例为大家分享了Java多线程实现复制文件的具体代码,供大家参考,具体内容如下

/**
 * 实现文件复制功能
 * 多线程实现文件从一个目录复制到另一个目录
 * @param sourceFile:给定源文件路径名
 * @param desPath:复制点文件路径
 * @return
 */

代码实现如下:

package com.tulun.thread;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;

/**
 * 多线程复制文件
 */
public class ThreadCopyFile {
    public static void main(String[] args) throws Exception {
        File file = new File("D:\\demo\\erke\\test.txt");
        startThread(5, file.length(), "D:\\demo\\erke\\test.txt",
                "D:\\demo\\erke\\test1.txt");
    }

    /**
     * 开启多线程复制
     * 
     * @param threadnum   线程数
     *  
     * @param fileLength   文件大小(用于确认每个线程下载多少东西)
     *            
     * @param sourseFilePath    源文件目录
     *           
     * @param desFilePath     目标文件目录
     *           
     */
    public static void startThread(int threadnum, long fileLength, String sourseFilePath, String desFilePath) {
        System.out.println(fileLength);
        long modLength = fileLength % threadnum;
        System.out.println("modLength:" + modLength);
        long desLength = fileLength / threadnum;
        System.out.println("desLength:" + desLength);
        for (int i = 0; i < threadnum; i++) {
            System.out.println((desLength * i) + "-----" + (desLength * (i + 1)));
            new FileWriteThread((desLength * i), (desLength * (i + 1)), sourseFilePath, desFilePath).start();
        }
        if (modLength != 0) {
            System.out.println("最后的文件写入");
            System.out.println((desLength * threadnum) + "-----" + (desLength * threadnum + modLength));
            new FileWriteThread((desLength * threadnum), desLength * threadnum + modLength + 1, sourseFilePath,
                    desFilePath).start();
        }
    }

    /**
     * 写线程:指定文件开始位置、目标位置、源文件、目标文件,
     */
    static class FileWriteThread extends Thread {
        private long begin;
        private long end;
        private RandomAccessFile sourseFile;
        private RandomAccessFile desFile;

        public FileWriteThread(long begin, long end, String sourseFilePath, String desFilePath) {
            this.begin = begin;
            this.end = end;
            try {
                this.sourseFile = new RandomAccessFile(sourseFilePath, "rw");
                this.desFile = new RandomAccessFile(desFilePath, "rw");
            } catch (FileNotFoundException e) {
            }
        }

        public void run() {
            try {
                sourseFile.seek(begin);
                desFile.seek(begin);
                int hasRead = 0;
                byte[] buffer = new byte[1];
                while (begin < end && -1 != (hasRead = sourseFile.read(buffer))) {
                     begin += hasRead;
                    desFile.write(buffer, 0, hasRead);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    sourseFile.close();
                    desFile.close();
                } catch (Exception e) {
                }
            }
        }
    }
}

运行结果:

上一篇:feignclient https 接口调用报证书错误的解决方案

栏    目:JAVA代码

下一篇:SpringCloud Stream 整合RabbitMQ的基本步骤

本文标题:Java多线程实现复制文件

本文地址:http://www.codeinn.net/misctech/227746.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有