IO 单个文件的多线程拷贝

Posted 柒禾

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IO 单个文件的多线程拷贝相关的知识,希望对你有一定的参考价值。

package FileCopyThread;                         //自建的包,根据个人调整

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class FileCopy {
    public static void main(String[] args) {
        
        int start = (int)System.currentTimeMillis();
        try {
            RandomAccessFile raf = new RandomAccessFile(new File("E:/11.txt"), "r");
            int fileLen = (int)raf.length();
            int nums = 20;
            int distance = (int)(fileLen/nums);
            
            new ThreadFileCopy(0, distance - 1).start();;
            for(int i = 1; i <= nums - 2; i++){
                new ThreadFileCopy(distance*i, distance*(i+1) - 1).start();
            }
            new ThreadFileCopy(distance*(nums-1), fileLen).start();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        int end = (int)System.currentTimeMillis();
        System.out.println("耗时:" + (end - start));
        
    }
}
package FileCopyThread;                         //自建的包根据个人调整

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class ThreadFileCopy extends Thread{
    final String oldPath = "E:/11.txt";
    final String newPath = "F:/22.txt";
    
    RandomAccessFile oraf = null;
    RandomAccessFile nraf = null;
    
    private int startPos = 0;
    private int endPos = 0;
    final int bufSize = 10240 * 5;
    public ThreadFileCopy(){
        
    }
    
    public ThreadFileCopy(int startPos, int endPos){
        this.startPos = startPos;
        this.endPos = endPos;
    }
    
    @Override
    public void run() {
        
        try {
            oraf = new RandomAccessFile(new File(oldPath), "r");
            nraf = new RandomAccessFile(new File(newPath), "rw");
            
            oraf.seek(startPos);
            nraf.seek(startPos);
            
            byte[] bytes = new byte[bufSize];
            int len;
            while(endPos - startPos > 0){
                len = (int)((endPos - startPos) > bufSize ? bufSize :(endPos - startPos));
                oraf.read(bytes, 0, len);
                nraf.write(bytes, 0, len);
                endPos -= len;
                System.out.println(Thread.currentThread().getName() 
                        + "读取了" + len + "个字节");
            }
            
            
            
            oraf.close();
            nraf.close();
            
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            
        }
    }
}

 

以上是关于IO 单个文件的多线程拷贝的主要内容,如果未能解决你的问题,请参考以下文章

带 bo​​ost asio 的多线程服务器

java之线程安全的单例类和文件拷贝

java的多线程:线程基础

套接字的多线程技术

Memcached 和 Redis

代码适用于与单个 html 文件不同的堆栈片段