博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javase多线程复制
阅读量:4655 次
发布时间:2019-06-09

本文共 3183 字,大约阅读时间需要 10 分钟。

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     @Test89     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

你可能感兴趣的文章
第二阶段站立会议7
查看>>
JAVA多线程
查看>>
delphi 更改DBGrid 颜色技巧
查看>>
POJ 2031 Building a Space Station
查看>>
面向对象1
查看>>
任意阶幻方(魔方矩阵)C语言实现
查看>>
织梦教程
查看>>
杭电多校 Harvest of Apples 莫队
查看>>
C/C++心得-结构体
查看>>
函数名作为参数传递
查看>>
apt-get for ubuntu 工具简介
查看>>
数值计算算法-多项式插值算法的实现与分析
查看>>
day8-异常处理与网络编程
查看>>
Python基础-time and datetime
查看>>
Linux epoll 笔记(高并发事件处理机制)
查看>>
shell脚本练习01
查看>>
WPF图标拾取器
查看>>
通过取父级for循环的i来理解闭包,iife,匿名函数
查看>>
HDU 3374 String Problem
查看>>
数据集
查看>>