亲宝软件园·资讯

展开

Java多线程复制文件

不忘初心珂 人气:0

/**
 * 实现文件复制功能
 * 多线程实现文件从一个目录复制到另一个目录
 * @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) {
                }
            }
        }
    }
}

运行结果:

加载全部内容

相关教程
猜你喜欢
用户评论