java檔案複製方式在100MB檔案條件下速度的比較

post200發表於2021-09-09
package com.imooc.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class IOUtil {
    public static void copyFile(File srcFile,File destFile)throws IOException{
        if(!srcFile.exists()){
            throw new IllegalArgumentException("檔案:"+srcFile+"不存在");
        }
        if(!srcFile.isFile()){
            throw new IllegalArgumentException(srcFile+"不是檔案");
        }
        FileInputStream in = new FileInputStream(srcFile);
        FileOutputStream out = new FileOutputStream(destFile);
        byte[] buf = new byte[8*1024];
        int b ;
        while((b = in.read(buf,0,buf.length))!=-1){
            out.write(buf,0,b);
            out.flush();
        }
        in.close();
        out.close();

    }

    public static void copyFileByBuffer(File srcFile,File destFile)throws IOException{
        if(!srcFile.exists()){
            throw new IllegalArgumentException("檔案:"+srcFile+"不存在");
        }
        if(!srcFile.isFile()){
            throw new IllegalArgumentException(srcFile+"不是檔案");
        }
        BufferedInputStream bis = new BufferedInputStream(
                new FileInputStream(srcFile));
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(destFile));
        int c ;
        while((c = bis.read())!=-1){
            bos.write(c);
            bos.flush();
        }
        bis.close();
        bos.close();
    }

    public static void copyFileByBuffer2(File srcFile,File destFile)throws IOException{
        if(!srcFile.exists()){
            throw new IllegalArgumentException("檔案:"+srcFile+"不存在");
        }
        if(!srcFile.isFile()){
            throw new IllegalArgumentException(srcFile+"不是檔案");
        }
        BufferedInputStream bis = new BufferedInputStream(
                new FileInputStream(srcFile));
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(destFile));
        byte[] buf = new byte[8*1024];
        int b ;
        while((b = bis.read(buf,0,buf.length))!=-1){
            bos.write(buf,0,b);
            bos.flush();
        }
        bis.close();
        bos.close();
    }
    public static void copyFileByByte(File srcFile,File destFile)throws IOException{
        if(!srcFile.exists()){
            throw new IllegalArgumentException("檔案:"+srcFile+"不存在");
        }
        if(!srcFile.isFile()){
            throw new IllegalArgumentException(srcFile+"不是檔案");
        }
        FileInputStream in = new FileInputStream(srcFile);
        FileOutputStream out = new FileOutputStream(destFile);
        int c ;
        while((c = in.read())!=-1){
            out.write(c);
            out.flush();
        }
        in.close();
        out.close();
    }

}
package com.imooc.io;

import java.io.File;
import java.io.IOException;

public class IOUtilTest4 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            File f=new File("e:\guanjujinqinpp.apk");
            //按位元組批次讀取方式複製檔案時間測試100MB以上
            long start = System.currentTimeMillis();
            IOUtil.copyFile(f, new File(
                    "e:\test1.apk"));
            long end = System.currentTimeMillis();
            System.out.println("檔案大小"+f.length()/1024/1024+"MBt"+"位元組批次讀取方式複製檔案所需時間"+(end - start)+"ms" );  
            //按批次緩衝方式讀取檔案複製檔案時間測試100MB以上
            start = System.currentTimeMillis();
            IOUtil.copyFileByBuffer2(f, new File(
                    "e:\test2.apk"));
            end = System.currentTimeMillis();
            System.out.println("檔案大小"+f.length()/1024/1024+"MBt"+"緩衝批次讀取方式複製檔案所需時間"+(end - start)+"ms" );

            //按緩衝方式讀取方式複製檔案測試100MB以上
            start = System.currentTimeMillis();
            IOUtil.copyFileByBuffer(f, new File(
                    "e:\test3.apk"));
            end = System.currentTimeMillis();
            System.out.println("檔案大小"+f.length()/1024/1024+"MBt"+"緩衝讀取方式複製檔案所需時間"+(end - start) +"ms");    

            //按位元組讀取方式複製檔案測試100MB以上
            start = System.currentTimeMillis();
            IOUtil.copyFileByByte(f, new File(
                    "e:\test4.apk"));
            end = System.currentTimeMillis();
            System.out.println("檔案大小"+f.length()/1024/1024+"MBt"+"位元組讀取方式複製檔案所需時間"+(end - start)+"ms" );
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

透過比較,可以看出批次複製的優勢,但是在100MB左右的檔案複製條件下,批次緩衝比批次位元組並沒有太大的優勢。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2249/viewspace-2798939/,如需轉載,請註明出處,否則將追究法律責任。

相關文章