javase多线程复制

 1 package multiThread;
 2 
 3 import java.io.File;
 4 import java.io.FileNotFoundException;
 5 import java.io.RandomAccessFile;
 6 
 7 import org.junit.Test;
 8 
 9 public class MultiThreadCopy {
10     public void copy(String srcFile, String destFile) {
11         File sf = new File(srcFile);
12         // 源文件大小
13         int length = (int) sf.length();
14         // 线程数
15         int count = 3;
16         // 每个线程复制的大小
17         int blockSize = length / count;
18         for (int i = 0; i < count; i++) {
19             int startPos = i * blockSize;
20             int endPos = 0;
21             if (i != (count - 1)) {
22                 endPos = (i + 1) * blockSize - 1;
23             } else {
24                 endPos = length - 1;
25             }
26             // 开线程
27             new ThreadCopy(srcFile, destFile, startPos, endPos).start();
28         }
29     }
30 
31     /**
32      * 复制线程
33      */
34     class ThreadCopy extends Thread {
35         private String srcFile;
36         private String destFile;
37         private int startPos;
38         private int endPos;
39 
40         public ThreadCopy(String srcFile, String destFile, int startPos, int endPos) {
41             super();
42             this.srcFile = srcFile;
43             this.destFile = destFile;
44             this.startPos = startPos;
45             this.endPos = endPos;
46         }
47 
48         public void run() {
49             try {
50                 // 源文件
51                 RandomAccessFile srcRaf = new RandomAccessFile(srcFile, "r");
52                 // 目标文件
53                 RandomAccessFile destRaf = new RandomAccessFile(destFile, "rw");
54                 // 定位读存位置
55                 srcRaf.seek(startPos);
56                 destRaf.seek(startPos);
57                 // 读取的字节数
58                 int bytes = endPos - startPos + 1;
59                 byte[] buf = new byte[1024];
60                 int len = 0;
61                 // 读取次数
62                 int count = bytes / buf.length;
63                 for (int i = 0; i < count; i++) {
64                     // 最后一次读取
65                     if (i == (count - 1)) {
66                         if ((bytes % buf.length) != 0) {
67                             buf = new byte[buf.length + (bytes % buf.length)];
68                             srcRaf.read(buf);
69                             destRaf.write(buf);
70                         } else {
71                             srcRaf.read(buf);
72                             destRaf.write(buf);
73                         }
74                         srcRaf.close();
75                         destRaf.close();
76 
77                     } else {
78                         srcRaf.read(buf);
79                         destRaf.write(buf);
80                     }
81                 }
82 
83             } catch (Exception e) {
84                 e.printStackTrace();
85             }
86         }
87     }
88     @Test
89     public static void main(String[] args){
90         MultiThreadCopy mt = new MultiThreadCopy();
91         String src="E:\大数据资料\文件\java基础\视频\day09\day01_01集合的复习.avi";
92         String des="E:\day01_01集合的复习.avi";
93         mt.copy(src, des);
94     }
95 }
原文地址:https://www.cnblogs.com/yihaifutai/p/6752036.html