当前位置:编程学习 > JAVA >>

java多线程复制文件

[java]  
package com.test;  
  
import java.io.FileNotFoundException;  
import java.io.IOException;  
import java.io.RandomAccessFile;  
  
public class FileCoper {  
    private static final String _ORIGIN_FILE_MODE = "r";  
  
       
     private static final String _TARGET_FILE_MODE = "rw";  
  
     private static long time1 = 0l;  
     private String originFileName;  
  
       
     private String targetFileName;  
  
       
     private RandomAccessFile originFile;  
  
       
     private RandomAccessFile targetFile;  
  
       
     private int threadCount;  
  
       
     private static int totalThreadCount = 0;  
       
     private static int executedCount = 0;  
  
     public FileCoper() {  
      this.threadCount = 1;  
      totalThreadCount = this.threadCount;  
     }  
  
     public FileCoper(String originFile, String targetFile) {  
      try {  
       this.originFileName = originFile;  
       this.targetFileName = targetFile;  
       this.originFile = new RandomAccessFile((originFile), FileCoper._ORIGIN_FILE_MODE);  
       this.targetFile = new RandomAccessFile((targetFile), FileCoper._TARGET_FILE_MODE);  
       this.threadCount = 1;  
       totalThreadCount = this.threadCount;  
      } catch (FileNotFoundException e) {  
       e.printStackTrace();  
      }  
     }  
  
     public FileCoper(String originFile, String targetFile, int threadCount) {  
      try {  
       this.originFileName = originFile;  
       this.targetFileName = targetFile;  
       this.originFile = new RandomAccessFile((originFile), FileCoper._ORIGIN_FILE_MODE);  
       this.targetFile = new RandomAccessFile((targetFile), FileCoper._TARGET_FILE_MODE);  
       this.threadCount = 1;  
       totalThreadCount = this.threadCount;  
      } catch (FileNotFoundException e) {  
       e.printStackTrace();  
      }  
     }  
  
       
     public void init(String originFile, String targetFile) throws Exception {  
      this.originFileName = originFile;  
      this.targetFileName = targetFile;  
      this.originFile = new RandomAccessFile((originFile), FileCoper._ORIGIN_FILE_MODE);  
      this.targetFile = new RandomAccessFile((targetFile), FileCoper._TARGET_FILE_MODE);  
      this.threadCount = 1;  
      totalThreadCount = this.threadCount;  
     }  
  
       
     public void init(String originFile, String targetFile, int threadCount) throws Exception {  
      this.originFileName = originFile;  
      this.targetFileName = targetFile;  
      this.originFile = new RandomAccessFile((originFile), FileCoper._ORIGIN_FILE_MODE);  
      this.targetFile = new RandomAccessFile((targetFile), FileCoper._TARGET_FILE_MODE);  
      this.threadCount = threadCount;  
      totalThreadCount = this.threadCount;  
     }  
  
       
     public void init(RandomAccessFile originFile, RandomAccessFile targetFile) throws Exception {  
      this.originFile = originFile;  
      this.targetFile = targetFile;  
      this.threadCount = 1;  
      totalThreadCount = this.threadCount;  
     }  
  
       
     public void init(RandomAccessFile originFile, RandomAccessFile targetFile, int threadCount) throws Exception {  
      this.originFile = originFile;  
      this.targetFile = targetFile;  
      this.threadCount = threadCount;  
      totalThreadCount = this.threadCount;  
     }  
  
       
     public static synchronized void finish() {  
      FileCoper.executedCount ++;  
        
      System.out.println("总线程【" + FileCoper.totalThreadCount + "】,已经完成【" + FileCoper.executedCount + "】个线程的复制!!!");  
      if (FileCoper.totalThreadCount == FileCoper.executedCount){  
          long time2 = System.currentTimeMillis();  
          System.out.println("花费时长:"+(time2-time1));  
           System.out.println("所有【" + FileCoper.totalThreadCount + "】线程复制完成!!!");  
      }  
     }  
  
       
     public void start() throws Exception {  
      if (this.originFile.length() == 0)  
       return;  
      if (this.threadCount == 0)  
       this.threadCount = 1;  
      // 设置目标文件大小  
      this.targetFile.setLength(this.originFile.length());  
      this.targetFile.seek(0);  
      this.originFile.seek(0);  
      time1 = System.currentTimeMillis();  
补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,