欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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");
? ? }

? ? /**
? ? ?* 開啟多線程復(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é)果:

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

相關(guān)文章

  • IDEA讓包分層顯示的實(shí)現(xiàn)方式

    IDEA讓包分層顯示的實(shí)現(xiàn)方式

    這篇文章主要介紹了IDEA讓包分層顯示的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 使用spring aop 統(tǒng)一捕獲異常和寫日志的示例demo

    使用spring aop 統(tǒng)一捕獲異常和寫日志的示例demo

    本文通過一個(gè)小demo給大家介紹spring AOP 實(shí)現(xiàn)的異常捕獲和日志的方法技巧,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-08-08
  • SpringBoot使用Druid數(shù)據(jù)源的配置方法

    SpringBoot使用Druid數(shù)據(jù)源的配置方法

    這篇文章主要介紹了SpringBoot使用Druid數(shù)據(jù)源的配置方法,文中代碼實(shí)例相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2018-04-04
  • Java如何替換RequestBody和RequestParam參數(shù)的屬性

    Java如何替換RequestBody和RequestParam參數(shù)的屬性

    近期由于接手的老項(xiàng)目中存在所有接口中新增一個(gè)加密串來給接口做一個(gè)加密效果,所以就研究了一下Http請(qǐng)求鏈路,發(fā)現(xiàn)可以通過?javax.servlet.Filter去實(shí)現(xiàn),這篇文章主要介紹了Java替換RequestBody和RequestParam參數(shù)的屬性,需要的朋友可以參考下
    2023-10-10
  • Java+mysql實(shí)現(xiàn)學(xué)籍管理系統(tǒng)

    Java+mysql實(shí)現(xiàn)學(xué)籍管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Java+mysql實(shí)現(xiàn)學(xué)籍管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • 基于Java多線程notify與notifyall的區(qū)別分析

    基于Java多線程notify與notifyall的區(qū)別分析

    本篇文章對(duì)Java中多線程notify與notifyall的區(qū)別進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下
    2013-05-05
  • Java整數(shù)和字符串相互轉(zhuǎn)化實(shí)例詳解

    Java整數(shù)和字符串相互轉(zhuǎn)化實(shí)例詳解

    這篇文章主要介紹了Java整數(shù)和字符串相互轉(zhuǎn)化實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • 用Java設(shè)計(jì)實(shí)現(xiàn)多實(shí)例多庫(kù)查詢方式

    用Java設(shè)計(jì)實(shí)現(xiàn)多實(shí)例多庫(kù)查詢方式

    這篇文章主要介紹了用Java設(shè)計(jì)實(shí)現(xiàn)多實(shí)例多庫(kù)查詢方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Java編程基礎(chǔ)測(cè)試題分享

    Java編程基礎(chǔ)測(cè)試題分享

    這篇文章主要介紹了Java編程基礎(chǔ)測(cè)試題分享,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10
  • Spring中SmartLifecycle的用法解讀

    Spring中SmartLifecycle的用法解讀

    這篇文章主要介紹了Spring中SmartLifecycle的用法解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09

最新評(píng)論