Java多線程實(shí)現(xiàn)復(fù)制文件
本文實(shí)例為大家分享了Java多線程實(shí)現(xiàn)復(fù)制文件的具體代碼,供大家參考,具體內(nèi)容如下
/**
* 實(shí)現(xiàn)文件復(fù)制功能
* 多線程實(shí)現(xiàn)文件從一個(gè)目錄復(fù)制到另一個(gè)目錄
* @param sourceFile:給定源文件路徑名
* @param desPath:復(fù)制點(diǎn)文件路徑
* @return
*/
代碼實(shí)現(xiàn)如下:
package com.tulun.thread;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
/**
?* 多線程復(fù)制文件
?*/
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");
? ? }
? ? /**
? ? ?* 開啟多線程復(fù)制
? ? ?*?
? ? ?* @param threadnum ? 線程數(shù)
? ? ?* ?
? ? ?* @param fileLength ? 文件大小(用于確認(rèn)每個(gè)線程下載多少東西)
? ? ?* ? ? ? ? ? ?
? ? ?* @param sourseFilePath ? ?源文件目錄
? ? ?* ? ? ? ? ??
? ? ?* @param desFilePath ? ? 目標(biāo)文件目錄
? ? ?* ? ? ? ? ??
? ? ?*/
? ? 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();
? ? ? ? }
? ? }
? ? /**
? ? ?* 寫線程:指定文件開始位置、目標(biāo)位置、源文件、目標(biāo)文件,
? ? ?*/
? ? 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) {
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
}運(yùn)行結(jié)果:

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA在plugins里搜不到mybatisx插件的解決方法
本文主要介紹了IDEA在plugins里搜不到mybatisx插件的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
關(guān)于ConditionalOnMissingBean失效問題的追蹤
這篇文章主要介紹了關(guān)于ConditionalOnMissingBean失效問題的追蹤方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringCloud連接不上遠(yuǎn)程N(yùn)acos問題排查
本文主要介紹了SpringCloud連接不上遠(yuǎn)程N(yùn)acos問題排查,可能是因?yàn)槲撮_放端口,或集群內(nèi)部通信異常等,下面就來介紹一下問題解決,感興趣的可以了解一下2024-06-06
Springboot?jpa使用sum()函數(shù)返回結(jié)果如何被接收
這篇文章主要介紹了Springboot?jpa使用sum()函數(shù)返回結(jié)果如何接收,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
用Java實(shí)現(xiàn)簡單ATM機(jī)功能
這篇文章主要為大家詳細(xì)介紹了用Java實(shí)現(xiàn)簡單ATM機(jī)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
springboot+mybatis-plus 兩種方式打印sql語句的方法
這篇文章主要介紹了springboot+mybatis-plus 兩種方式打印sql語句的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
SpringBoot設(shè)置Session失效時(shí)間的解決方案
當(dāng)過期時(shí)間是大于1分鐘的時(shí)候是沒有什么問題的,但是如果設(shè)置過期時(shí)間小于1分鐘,就會(huì)失效,這篇文章主要介紹了SpringBoot設(shè)置Session失效時(shí)間的解決方案,需要的朋友可以參考下2024-05-05
java的新特性反射機(jī)制應(yīng)用及操作示例詳解
這篇文章主要為大家介紹了java的新特性反射機(jī)制的操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05

