最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java多線程實(shí)現(xiàn)復(fù)制文件

 更新時(shí)間:2022年04月07日 09:53:50   作者:不忘初心珂  
這篇文章主要為大家詳細(xì)介紹了Java多線程實(shí)現(xiàn)復(fù)制文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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");
? ? }

? ? /**
? ? ?* 開(kāi)啟多線程復(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("最后的文件寫(xiě)入");
? ? ? ? ? ? System.out.println((desLength * threadnum) + "-----" + (desLength * threadnum + modLength));
? ? ? ? ? ? new FileWriteThread((desLength * threadnum), desLength * threadnum + modLength + 1, sourseFilePath,
? ? ? ? ? ? ? ? ? ? desFilePath).start();
? ? ? ? }
? ? }

? ? /**
? ? ?* 寫(xiě)線程:指定文件開(kāi)始位置、目標(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é)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • JAVA SPI機(jī)制詳解使用方法

    JAVA SPI機(jī)制詳解使用方法

    Java定義了一套JDBC的接口,但并未提供具體實(shí)現(xiàn)類(lèi),而是在不同云廠商提供的數(shù)據(jù)庫(kù)實(shí)現(xiàn)包。這篇文章給大家介紹Java的SPI機(jī)制,感興趣的朋友一起看看吧
    2022-07-07
  • IDEA在plugins里搜不到mybatisx插件的解決方法

    IDEA在plugins里搜不到mybatisx插件的解決方法

    本文主要介紹了IDEA在plugins里搜不到mybatisx插件的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • 關(guān)于ConditionalOnMissingBean失效問(wèn)題的追蹤

    關(guān)于ConditionalOnMissingBean失效問(wèn)題的追蹤

    這篇文章主要介紹了關(guān)于ConditionalOnMissingBean失效問(wèn)題的追蹤方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • SpringCloud連接不上遠(yuǎn)程N(yùn)acos問(wèn)題排查

    SpringCloud連接不上遠(yuǎn)程N(yùn)acos問(wèn)題排查

    本文主要介紹了SpringCloud連接不上遠(yuǎn)程N(yùn)acos問(wèn)題排查,可能是因?yàn)槲撮_(kāi)放端口,或集群內(nèi)部通信異常等,下面就來(lái)介紹一下問(wèn)題解決,感興趣的可以了解一下
    2024-06-06
  • Springboot?jpa使用sum()函數(shù)返回結(jié)果如何被接收

    Springboot?jpa使用sum()函數(shù)返回結(jié)果如何被接收

    這篇文章主要介紹了Springboot?jpa使用sum()函數(shù)返回結(jié)果如何接收,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 用Java實(shí)現(xiàn)簡(jiǎn)單ATM機(jī)功能

    用Java實(shí)現(xiàn)簡(jiǎn)單ATM機(jī)功能

    這篇文章主要為大家詳細(xì)介紹了用Java實(shí)現(xiàn)簡(jiǎn)單ATM機(jī)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • JAVA List和Map切割工具詳解

    JAVA List和Map切割工具詳解

    這篇文章主要介紹了JAVA List和Map切割工具詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • springboot+mybatis-plus 兩種方式打印sql語(yǔ)句的方法

    springboot+mybatis-plus 兩種方式打印sql語(yǔ)句的方法

    這篇文章主要介紹了springboot+mybatis-plus 兩種方式打印sql語(yǔ)句的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • SpringBoot設(shè)置Session失效時(shí)間的解決方案

    SpringBoot設(shè)置Session失效時(shí)間的解決方案

    當(dāng)過(guò)期時(shí)間是大于1分鐘的時(shí)候是沒(méi)有什么問(wèn)題的,但是如果設(shè)置過(guò)期時(shí)間小于1分鐘,就會(huì)失效,這篇文章主要介紹了SpringBoot設(shè)置Session失效時(shí)間的解決方案,需要的朋友可以參考下
    2024-05-05
  • java的新特性反射機(jī)制應(yīng)用及操作示例詳解

    java的新特性反射機(jī)制應(yīng)用及操作示例詳解

    這篇文章主要為大家介紹了java的新特性反射機(jī)制的操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05

最新評(píng)論

莎车县| 驻马店市| 青铜峡市| 霞浦县| 枣强县| 长寿区| 金乡县| 道真| 始兴县| 玉龙| 安远县| 青铜峡市| 江山市| 鹤山市| 中宁县| 德兴市| 平山县| 和龙市| 中方县| 墨脱县| 青浦区| 宁都县| 临泉县| 达拉特旗| 莒南县| 大石桥市| 台州市| 河东区| 台中市| 布尔津县| 内丘县| 高雄县| 潜山县| 和田市| 英山县| 浙江省| 固原市| 通河县| 马山县| 育儿| 石阡县|