• 快速拷贝文件


    package com.demo;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    
    public class QuickCopy {
    
        private static final int threshold = 10*1024;
        
        private static final int max_buf = 1024;
        
        private static final int max_worker = 4;
        
        private File targetFile;
        
        private String targetFileName;
        
        public String getTargetFileName() {
            return targetFileName;
        }
    
        public void setTargetFileName(String targetFileName) {
            this.targetFile = new File(targetFileName);
            
            this.targetFileName = targetFileName;
        }
    
        private File sourceFile;
        
        private String sourceFileName;
    
        public String getSourceFileName() {
            return sourceFileName;
        }
    
        public void setSourceFileName(String sourceFileName) throws FileNotFoundException {
            this.sourceFile = new File(sourceFileName);
            if(!this.sourceFile.exists() && !this.sourceFile.isFile()) {
                throw new FileNotFoundException();
            }
            this.sourceFileName = sourceFileName;
        }
        
        public QuickCopy(String sourceFileName, String targetFileName) throws FileNotFoundException {
            this.setSourceFileName(sourceFileName);
            this.setTargetFileName(targetFileName);
        }
        
        public void copy() throws IOException{
            createTargetFile();
            if(this.sourceFile.length() > threshold) {
                multiCopy();
            } else {
                singleCopy();
            }
        }
        
        private void createTargetFile() throws IOException {
            long length = this.sourceFile.length();
            File parent = this.targetFile.getParentFile();
            if(parent != null && !parent.exists()) {
                parent.mkdirs();
            }
            RandomAccessFile raf = 
                    new RandomAccessFile(
                            this.targetFile.getAbsolutePath(), "rw");
            raf.setLength(length);
            raf.close();
        }
        
        private void singleCopy() throws IOException {
            FileInputStream in = new FileInputStream(this.sourceFile);
            FileOutputStream out = new FileOutputStream(this.targetFile);
            byte[] buf = new byte[max_buf];
            int read = 0;
            while((read = in.read(buf)) > 0) {
                out.write(buf, 0, read);
            }
            in.close();
            out.close();
        }
        
        private void multiCopy() throws IOException {
            long length = this.sourceFile.length();
            long ave = length / max_worker;
            for(int i=0; i<max_worker; i++) {
                if(i == max_worker-1) {
                    multiCopy(i*ave, ave, false);
                } else {
                    multiCopy(i*ave, ave, true);
                }
            }
        }
        
        private void multiCopy(long start, long total, boolean flag) {
            Thread th = new Thread(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        long ntotal = total;
                        FileInputStream in = 
                                new FileInputStream(sourceFile);
                        RandomAccessFile out = 
                                new RandomAccessFile(targetFile, "rw");
                        try {
                            in.skip(start);
                            out.seek(start);
                            byte[] buf = new byte[max_buf];
                            int read = 0;
                            while(ntotal > 0 
                                    && (read = in.read(buf, 0, 
                                        !flag ? max_buf:
                                        (int)(ntotal >= max_buf ? max_buf : max_buf-ntotal))) > 0) 
                            {
                                out.write(buf, 0, read);
                                
                                if(flag) {
                                    ntotal -= read;
                                }
                            }
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        } finally {
                            in.close();
                            out.close();
                        }
                        
                        
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                
            });
            th.start();
        }
    }
  • 相关阅读:
    koa 放置 前台打包dist 目录
    tomcat startup.bat 包含springboot的输出 里面乱码的解决方案
    base64 转文件上传
    4时4态 加被动 例句:I will have been being done
    软件推荐 Notable / 现改用 Vnote 了
    [win10] 开始-设置 / 右键-显示设置 / 右键个性化 等都不好使了。。 ms-settings:display
    viewui tree 自定义化(源码copy出来改动)#添加 获取selected 解决方案
    idea 暂存 Stash Changes Git/Repository/Stash Changes 恢复暂存 UnStash Changes
    vm 虚拟机总是蓝屏 移除打印机和声卡 移除这俩硬件 (大文件用飞秋传输)
    docker中mysql 汉字乱码,显示问号
  • 原文地址:https://www.cnblogs.com/lvniao/p/9449981.html
Copyright © 2020-2023  润新知