Java多线程实现复制文件
作者:不忘初心珂 时间:2023-03-21 07:54:17
本文实例为大家分享了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) {
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
运行结果:
来源:https://blog.csdn.net/qq_41930448/article/details/82829681
标签:Java,多线程,复制
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用Spring Boot AOP处理方法的入参和返回值
2022-03-02 07:25:41
![](https://img.aspxhome.com/file/2023/3/87363_0s.jpg)
JNI语言基本知识
2022-01-20 20:02:46
使用设计模式中的工厂方法模式进行C#编程的示例讲解
2023-10-21 05:27:08
![](https://img.aspxhome.com/file/2023/0/96300_0s.png)
c#遍历System.drawing.Color下面的所有颜色以及名称以查看
2022-05-12 06:33:19
Jenkins自动化打包为war包
2021-08-08 20:25:49
![](https://img.aspxhome.com/file/2023/2/103992_0s.jpg)
MyBatis多对多关联映射创建示例
2023-08-09 06:40:10
![](https://img.aspxhome.com/file/2023/0/128910_0s.jpg)
Android NDK开发(C语言字符串)
2023-06-18 10:21:28
Java 接口和抽象类的区别详解
2021-07-26 13:30:21
Tomcat内存溢出分析及解决方法
2023-11-12 23:24:47
详解Java高级特性之反射
2021-06-09 14:01:06
Android用 Mob 实现发送短信验证码实例
2021-10-17 03:53:16
![](https://img.aspxhome.com/file/2023/4/106814_0s.jpg)
JAVA发送HTTP请求的四种方式总结
2023-08-23 20:27:23
android获取屏幕高度和宽度的实现方法
2023-05-31 04:45:58
Java多线程之CAS算法实现线程安全
2022-12-09 17:53:53
Android编程实现的手写板和涂鸦功能
2022-01-04 19:20:52
![](https://img.aspxhome.com/file/2023/8/101868_0s.png)
SpringBoot配置及使用Schedule过程解析
2021-12-31 18:47:32
SpringBoot实现拦截器、过滤器、监听器过程解析
2023-07-01 02:34:52
java定义二维数组的几种写法(小结)
2021-07-05 11:52:09
java使用hadoop实现关联商品统计
2022-11-05 05:55:43
![](https://img.aspxhome.com/file/2023/3/76603_0s.jpg)
C#中把英文字母转换为大写或小写的方法
2021-06-13 18:46:55