java FileI(O)nputStream為什麼比BufferedI(O)utputStream慢?

劍握在手發表於2013-12-05

因為buffered多了一個緩衝區,讀和寫都是先把硬碟或者記憶體中的資料放到記憶體中一塊快取區域,到一定大小讀寫到硬碟或者記憶體

 

package io;

import java.io.*;

public class FileIOTest {

    /**
     * @param args
     * @throws FileNotFoundException
     */
    public static void main(String[] args) throws Exception {

        //有buff的File***Stream
        System.out.println("有buff的File***Stream耗時:");
        new TimeTest() {
            void run()throws Exception{
                FileInputStream fis = new FileInputStream("F:\\Test\\file.zip");

                FileOutputStream fos = new FileOutputStream("F:\\Test\\file1.zip");

                byte[] buf = new byte[1024];

                int length = 0;


                while ((length = fis.read(buf)) > 0) {
                    fos.write(buf, 0, length);
                }

                fis.close();
                fos.close();

            }
        }.getTime();
       
        //有buff的Buffered***Stream
        System.out.println("有buff的Buffered***Stream耗時:");
        new TimeTest() {
            void run()throws Exception{

                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\Test\\file.zip"));

                BufferedOutputStream bos = new BufferedOutputStream(
                        new FileOutputStream("F:\\Test\\file2.zip"));
               
                byte[] buf = new byte[1024];

                int length = 0;
               
                while ((length = bis.read(buf)) > 0) {
                    bos.write(buf, 0, length);
                }

                bis.close();
                bos.close();
               
            }
        }.getTime();
       
        //無buff的File***Stream
        System.out.println("無buff的File***Stream耗時:");
        new TimeTest() {
            void run()throws Exception{
                FileInputStream fis = new FileInputStream("F:\\Test\\file.zip");

                FileOutputStream fos = new FileOutputStream("F:\\Test\\file3.zip");

                int data = 0;

                while ((data = fis.read()) !=-1) {
                    fos.write(data);
                }

                fis.close();
                fos.close();
            }
        }.getTime();
       
        //無buff的Buffered***Stream
        System.out.println("無buff的Buffered***Stream耗時:");
        new TimeTest() {
            void run()throws Exception{
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\Test\\file.zip"));

                BufferedOutputStream bos = new BufferedOutputStream(
                        new FileOutputStream("F:\\Test\\file4.zip"));
               
                int data = 0;
               
                int i =bis.available();
               
                while ((data = bis.read()) !=-1) {
                    bos.write((byte)data);
                }

                bis.close();
                bos.close();
            }
        }.getTime();
    }

}

//抽象的不太好的模板設計模式
abstract class TimeTest {

    void getTime() {
        long start = System.currentTimeMillis();
        try {
            run();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(System.currentTimeMillis() - start);
    }

    abstract void run() throws Exception;
}

 

 

測試資料248kb

 

測試結果:

 

有buff的File***Stream耗時:
8
有buff的Buffered***Stream耗時:
2
無buff的File***Stream耗時:
1369
無buff的Buffered***Stream耗時:
14

相關文章