亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java多線程實現(xiàn)復制文件

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

本文實例為大家分享了Java多線程實現(xiàn)復制文件的具體代碼,供大家參考,具體內容如下

/**
 * 實現(xiàn)文件復制功能
 * 多線程實現(xiàn)文件從一個目錄復制到另一個目錄
 * @param sourceFile:給定源文件路徑名
 * @param desPath:復制點文件路徑
 * @return
 */

代碼實現(xiàn)如下:

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 ? 線程數(shù)
? ? ?* ?
? ? ?* @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) {
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
}

運行結果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    用Java設計實現(xiàn)多實例多庫查詢方式

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

    Java編程基礎測試題分享

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

    Spring中SmartLifecycle的用法解讀

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

最新評論