• 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 {
                
            }
        }
    }
  • 相关阅读:
    ELM学习
    《进化》从孤胆极客到高效团队
    《人件》《PeopleWare》 【美】Tom DeMarco TimothyLister 著 肖然 张逸 滕云 译
    《进化》从孤胆极客到高效团队---Notes1
    大数据第一部分LInux学习Note1
    C#Windows窗体初学
    C#初学笔记(Windows编程的基本概念)
    C#学习2017-9-26(读取文本文件和读取二进制文件)Notes9
    C#学习2017-9-26Notes8(文件和流,FileStream类)
    C#学习笔记Notes8(接口,接口实现,程序集,命名空间,using)
  • 原文地址:https://www.cnblogs.com/854594834-YT/p/10527481.html
Copyright © 2020-2023  润新知