java 1G大文件復制


對比幾種復制方法

復制的文件是980m的txt文件

 

1、  FileChannel 方法

代碼:

  

public static void mappedBuffer() throws IOException{  
        long start=System.currentTimeMillis();
        FileChannel read = new FileInputStream("n2.txt").getChannel();   
        FileChannel writer = new RandomAccessFile("n5.txt","rw").getChannel();   
        long i = 0;   
        long size = read.size()/30;   
        ByteBuffer bb,cc = null;   
        while(i<read.size()&&(read.size()-i)>size){   
            bb = read.map(FileChannel.MapMode.READ_ONLY, i, size);   
            cc = writer.map(FileChannel.MapMode.READ_WRITE, i, size);   
            cc.put(bb);   
            i+=size;   
            bb.clear();   
            cc.clear();   
        }   
        bb = read.map(FileChannel.MapMode.READ_ONLY, i, read.size()-i);   
        cc.put(bb);   
        bb.clear();   
        cc.clear();   
        read.close();   
        writer.close();   
        long end=System.currentTimeMillis();
        System.out.println("用時:"+(end-start)+"毫秒");
    }  

 

耗時:807ms

 

使用NewIO技術復制大文件的速度最快,尤其是此方法中使用了內存映射技術,速度非常快。

 

2、  FileInputStream技術

public static void fileCopy(String srcFile,String tarFile)throws IOException{
        long start=System.currentTimeMillis();
        FileInputStream fis = null;
        FileOutputStream fos = null;
        File f =new File(srcFile);
        fis = new FileInputStream(f);
        fos = new FileOutputStream(tarFile);
        int len=0;
        byte[] b =new byte[t];
        while((len=fis.read(b))!=-1){
            fos.write(b);
            fos.flush();
        }
        long end=System.currentTimeMillis();
        System.out.println("用時:"+(end-start)+"毫秒");
        
        if(fis!=null){
            fis.close();
        }
        if(fos!=null){
            fos.close();
        }
    }

 

耗時:

 

 

1072ms,速度也不慢,在處理文本文檔的時候傳統的io技術速度並不慢,但如果處理的是圖像流文件,速度比NIO技術慢很多。

 

3、  BufferedOutputStream

 比起FileInputStream多了一層包裝

public static void fileCopy2(String srcFile,String tarFile)throws IOException{
        long start=System.currentTimeMillis();
        BufferedOutputStream fos = new BufferedOutputStream (new FileOutputStream(new File (tarFile)));
        BufferedInputStream fis = new BufferedInputStream (new FileInputStream(new File (srcFile)));
        int len=0;
        byte[] b =new byte[t];
        while((len=fis.read(b))!=-1){
            fos.write(b);
            fos.flush();
        }
        long end=System.currentTimeMillis();
        System.out.println("用時:"+(end-start)+"毫秒");
        
        if(fis!=null){
            fis.close();
        }
        if(fos!=null){
            fos.close();
        }
    }

 

耗時:

 

 

耗時問1175ms比FileInputStream慢了100ms(此處比較的前提是緩存數組大小一致 為100000)

 

4、  BufferedReader

public static void bufferedReader(String srcFile,String tarFile)throws IOException{
        long start=System.currentTimeMillis();
        BufferedReader br =new BufferedReader(new FileReader(new File(srcFile)));
        BufferedWriter fr =new BufferedWriter(new FileWriter(new File(tarFile)));
        int len = 0;
        char[] ch =new char[t];
        while((len=br.read(ch))!=-1){
            fr.write(ch);
        }
        long end=System.currentTimeMillis();
        System.out.println("用時:"+(end-start)+"毫秒");
        br.close();
        fr.close();
    }

 

耗時足足達到50s,比起前幾種方法簡直天差地別,但此參數並非最優參數,如果改變數組大小,速度能明顯提升

可比起前面的方法還是差了很遠。

 

5、  FileReader

public static void bufferedReader2(String srcFile,String tarFile)throws IOException{
        long start=System.currentTimeMillis();
        FileReader br =new FileReader(new File(srcFile));
        FileWriter fr =new FileWriter(new File(tarFile));
        int len = 0;
        char[] ch =new char[t];
        while((len=br.read(ch))!=-1){
            fr.write(ch);
        }
        long end=System.currentTimeMillis();
        System.out.println("用時:"+(end-start)+"毫秒");
        br.close();
        fr.close();
    }

此方法比起BufferedReader少了一層包裝,速度也更快些

經過測試發現此方法的速度受數組大小的影響程度不大

 

此份文檔中所有的方法的參數雖不是最優,但各種方法之間的速度有明顯的差距,就不再累贅逐一尋找最優參數了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM