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(); 
      System.out.println(this.originFile.length()); 
      // 把文件分块,每一块有一对值:当前块在文件中的起始位置和结束位置  
      long[][] splits = new long[this.threadCount][2]; 
      long originFileLength = this.originFile.length(); 
      int startPos = 0; 
      for (int i = 0; i < this.threadCount; i++) { 
       splits[i][0] = 0; 
       splits[i][1] = 0; 
       if (i == 0) { 
        splits[i][0] = 0; 
        splits[i][1] = originFileLength / this.threadCount; 

       } else if (i == this.threadCount - 1) { 
        // 注意:此处不能加1,如果加1,线程多文件就会出现乱码  
        // splits[i][0] = startPos + 1;  
        splits[i][0] = startPos; 
        splits[i][1] = originFileLength; 
       } else { 
        // 注意:此处不能加1,如果加1,线程多文件就会出现乱码  
        // splits[i][0] = startPos + 1;  
        splits[i][0] = startPos; 
        splits[i][1] = startPos + originFileLength / this.threadCount; 
       } 
       startPos += originFileLength / this.threadCount; 
       // System.out.println(splits[i][0] + " " + splits[i][1]);  

       Coper fc = new Coper("thread-" + i); 
       fc.init(this.originFile, this.targetFile, splits[i][0], splits[i][1]); 
       fc.setOriginFileName(this.originFileName); 
       fc.setTargetFileName(this.targetFileName); 
       fc.start(); 
      } 
     } 

      
     public void startNew() throws Exception { 
      if (this.originFile.length() == 0) 
       return; 
      // 设置目标文件大小  
      this.targetFile.setLength(this.originFile.length()); 
      this.targetFile.seek(0); 
      this.originFile.seek(0); 

      long startPosition; 
      long endPosition; 
      long block = this.originFile.length() / 1029; 

      if (block <= 1) 
       this.threadCount = 1; 

      for (int i = 0; i < this.threadCount; i++) { 
       // 定义每次转移的长度  
       startPosition = i * 1029 * (block / this.threadCount); 
       endPosition = (i + 1) * 1029 * (block / this.threadCount); 
       if (i == (this.threadCount - 1)) 
        endPosition = this.originFile.length(); 
       Coper fc = new Coper("thread-" + i); 
       fc.init(this.originFile, this.targetFile, startPosition, endPosition); 
       fc.setOriginFileName(this.originFileName); 
       fc.setTargetFileName(this.targetFileName); 
       fc.start(); 

      } 
     } 

     private class Coper extends Thread { 

       
      private String originFileName; 

       
      private String targetFileName; 

      private RandomAccessFile originFile; 

       
      private RandomAccessFile targetFile; 

       
      private String threadId; 

       
      private long startPosition; 

       
      private long endPosition; 

       
      private long blockCapacity; 

       
      public void setOriginFileName(String originFileName) { 
       this.originFileName = originFileName; 
      } 

       
      public void setTargetFileName(String targetFileName) { 
       this.targetFileName = targetFileName; 
      } 

      public Coper(String threadId) { 
       this.threadId = threadId; 
      } 

       
      public void init(RandomAccessFile originFile, RandomAccessFile targetFile, long startPosition, long endPosition) throws Exception { 
       this.originFile = originFile; 
       this.targetFile = targetFile; 
       this.startPosition = startPosition; 
       this.endPosition = endPosition; 
       this.blockCapacity = this.endPosition - this.startPosition; 
      } 

      public void run() { 
       // System.out.println(this.threadId + " 启动,开始复制文件【" +  
       // this.originFileName + "】中的文件块【" + this.startPosition + "," +  
       // this.endPosition + "】到目标文件【" + this.targetFileName + "】中...");  
       synchronized (this.originFile) { 
        try { 
         // 记录当前拷贝的字节数  
         int copyCount = 0; 
         // 数据拷贝的启示偏移量  
         long offSet = this.startPosition; 
         byte[] b = new byte[16 * 1024 * 1024]; 
         // 动态设置一次读取的字节数缓冲  
         long blockSize = 0; 
         while (copyCount < this.blockCapacity) { 
          this.originFile.seek(offSet); 
          if (this.blockCapacity - copyCount > 16 * 1024 * 1024) 
           blockSize = 16 * 1024 * 1024; 
          else 
           blockSize = this.blockCapacity - copyCount; 
          if (blockSize > this.blockCapacity - copyCount) 
           blockSize = this.blockCapacity - copyCount; 
          int count = this.originFile.read(b, 0, (int) blockSize); 
          synchronized (this.targetFile) { 
           try { 
            if (copyCount == 0) 
             this.targetFile.seek(offSet); 
            else 
             this.targetFile.seek(offSet + 1); 

            this.targetFile.write(b, 0, count); 
           } catch (IOException e) { 
            e.printStackTrace(); 
           } 
          } 
          // 增加拷贝的字节数  
          copyCount += count; 
          // 拷贝其实【偏移量下移  
          offSet += count; 
         } 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
       // System.out.println(this.threadId + " 复制文件【" + this.originFileName  
       // + "】中的文件块【" + this.startPosition + "," + this.endPosition +  
       // "】到目标文件【" + this.targetFileName + "】完成!");  

       // 通知主线程,当前线程完成复制工作  
       FileCoper.finish(); 
      } 

     } 

     public static void main(String[] args) throws Exception { 
      FileCoper fc = new FileCoper(); 
      fc.init("e:/InitialData_zhihuan.sql", "e:/InitialData_zhihuan2.sql", 30); 
      //fc.init("d:/ValueAdd_11.txt", "d:/ValueAdd_111.txt", 100);  
      // fc.init("D:\tools\music\做你的爱人.mp3", "d:/做你的爱人_5.mp3", 10);  
      //fc.init("E:\电影\最黑暗侵袭.rmvb", "d:/最黑暗侵袭_1.rmvb", 100);  

     /* // 读入键盘输入
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      // 文件来源
      String originFile;
      // 文件目标
      String targetFile;
      System.out.println("【源文件、目标文件、线程数】");
      System.out.print("要复制的源文件:");
      originFile = br.readLine();
      System.out.print("文件复制到目标文件:");
      targetFile = br.readLine();
      System.out.print("切分线程数:");
      int threadCount = Integer.parseInt(br.readLine());
      fc.init(originFile, targetFile, threadCount);*/ 
      // fc.startNew();  
      long time1 = System.currentTimeMillis(); 
      fc.start(); 
      long time2 = System.currentTimeMillis(); 
      System.out.println(time2-time1); 
     } 

时间: 2024-09-17 17:28:54

java多线程复制文件的实例代码_java的相关文章

Java实现FTP服务器功能实例代码_java

FTP(File Transfer Protocol 文件传输协议)是Internet 上用来传送文件的协议.在Internet上通过FTP 服务器可以进行文件的上传(Upload)或下载(Download).FTP是实时联机服务,在使用它之前必须是具有该服务的一个用户(用户名和口令),工作时客户端必须先登录到作为服务器一方的计算机上,用户登录后可以进行文件搜索和文件传送等有关操作,如改变当前工作目录.列文件目录.设置传输参数及传送文件等.使用FTP可以传送所有类型的文件,如文本文件.二进制可执

java哈夫曼树实例代码_java

本文实例为大家分享了哈夫曼树java代码,供大家参考,具体内容如下 package boom; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Queue; class Node<T> implements Comparable<Node<T>>{ private T

Java压缩文件ZIP实例代码_java

提示:java.util.zipoutputstream         java API压缩为zip文件 代码: 复制代码 代码如下: package com.gaoqi.test;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.zip.ZipEntry;import java.util.z

java读取csv文件内容示例代码_java

复制代码 代码如下: package com.huateng.readcsv; import java.io.BufferedReader;import java.io.FileReader;import java.util.ArrayList;import java.util.Iterator;import java.util.List; public class CsvUtil {        private String fileName = null;        private B

Java批量修改文件名的实例代码_java

复制代码 代码如下: import java.io.*; import java.util.*;public class Test {     public static void main(String[] args) throws IOException {        BufferedReader br = new BufferedReader(new FileReader("output1.txt"));        List<String> newName =

java实现目录文件监控实例代码

Java 7之前的版本都是没有原生支持的,下面就说说JDK 1.7 以下版本平台的解决方案: 事件驱动方式,无需无目录扫描,但与平台相关 线程轮询扫描,纯java实现,完美跨平台,但监听文件较多时,扫描的量太大,响应不是非常及时,依赖于扫描间隔时间 JNotify属于事件驱动,所以不同的平台的处理方式不一样.所以需要在java.library.path里面添加jnotify.dll/jnotify_64bit.dll,最简单的解决方案直接修改代码.JNotify_win32.java的47和51

java 实现约瑟夫环的实例代码_java

复制代码 代码如下: import java.io.BufferedInputStream;import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class Josephus {    private static class Node{        int No;        Node next;        public Node(int No){            this

Java实现邮箱找回密码实例代码_java

通过邮件找回密码功能的实现 1.最近开发一个系统,有个需求就是,忘记密码后通过邮箱找回.现在的系统在注册的时候都会强制输入邮箱,其一目的就是 通过邮件绑定找回,可以进行密码找回.通过java发送邮件的功能我就不说了,重点讲找回密码. 2.参考别人的思路:发送邮件→请求邮件里的URL→验证url→{验证成功修改密码,不成功跳转到失败页面} 重点就是如何生成这个url和如何解析这个url. 需要注意的是一个url只能修改一次密码,当同一帐号发送多封邮件,只有最后一封邮件的url 邮箱  3.加密能防

java实现MD5加密算法的实例代码_java

复制代码 代码如下: package other; import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;/* * MD5 算法*/public class MD5 {     // 全局数组    private final static String[] strDigits = { "0", "1", "2", "3"