Java中通過緩衝區提高I/O系能

來源:互聯網
上載者:User

我們知道Java中一般的輸入輸出資料流類都是用單位元組的讀取方法來進行I/O操作的,也就是說每次唯讀寫一個位元組的資料,這種方法顯然繁瑣低效。如果從裝置讀取10M的檔案,每次讀取一個位元組,完成操作將需要做10M/次I/O操作,I/O操作又是一件相當耗時的事情,無疑在很大程度上降低了系統的效能。

 

 

Java代碼 {
dp.sh.Toolbar.CopyToClipboard(this);return false;
}" href="http://writeblog.csdn.net/#">
  1. import java.io.*;   
  2.   
  3. /*******************************************************************************  
  4.  *   
  5.  * @author pengcqu  
  6.  *   
  7.  */  
  8. public class TestBuffer {   
  9.   
  10.     public static void main(String args[]) throws IOException {   
  11.         TestBuffer br = new TestBuffer();   
  12.         String from = "d:/a/1.MP3";   
  13.         long startTime = System.currentTimeMillis();   
  14.         br.readWrite(from,"d:/b/2.MP3");   
  15.         long endTime = System.currentTimeMillis();   
  16.         System.out.println("直接讀取耗時:" + (endTime - startTime) +"ms");   
  17.         long startTime1 = System.currentTimeMillis();   
  18.         br.readWriteWithBuffer(from, "d:/b/3.MP3");   
  19.         long endTime1 = System.currentTimeMillis();   
  20.         System.out.println("使用緩衝區讀取耗時:" + (endTime1 - startTime1) +"ms");   
  21.            
  22.   
  23.     }   
  24.   
  25.     /***************************************************************************  
  26.      * 直接讀取檔案  
  27.      *   
  28.      * @param from  
  29.      * @param to  
  30.      * @throws IOException  
  31.      */  
  32.     public static void readWrite(String from, String to) throws IOException {   
  33.         InputStream in = null;   
  34.         OutputStream out = null;   
  35.         try {   
  36.             in = new FileInputStream(from);   
  37.             out = new FileOutputStream(to);   
  38.             while (true) {   
  39.                 int data = in.read();   
  40.                 if (data == -1) {   
  41.                     break;   
  42.                 }   
  43.                 out.write(data);   
  44.             }   
  45.         } finally {   
  46.             if (in != null) {   
  47.                 in.close();   
  48.             }   
  49.             if (out != null) {   
  50.                 out.close();   
  51.             }   
  52.         }   
  53.     }   
  54.   
  55.     /***************************************************************************  
  56.      * 使用緩衝區讀寫檔案  
  57.      * @param from  
  58.      * @param to  
  59.      * @throws IOException  
  60.      */  
  61.     public static void readWriteWithBuffer(String from, String to)   
  62.             throws IOException {   
  63.         InputStream inBuffer = null;   
  64.         OutputStream outBuffer = null;   
  65.         try {   
  66.             inBuffer = new BufferedInputStream(new FileInputStream(from));   
  67.             outBuffer = new BufferedOutputStream(new FileOutputStream(to));   
  68.             while (true) {   
  69.                 int data = inBuffer.read();   
  70.                 if (data == -1) {   
  71.                     break;   
  72.                 }   
  73.                 outBuffer.write(data);   
  74.             }   
  75.         } finally {   
  76.             if (inBuffer != null) {   
  77.                 inBuffer.close();   
  78.             }   
  79.             if (outBuffer != null) {   
  80.                 outBuffer.close();   
  81.             }   
  82.         }   
  83.     }   
  84.   
  85. }  
import java.io.*;/******************************************************************************* *  * @author pengcqu *  */public class TestBuffer {public static void main(String args[]) throws IOException {TestBuffer br = new TestBuffer();String from = "d:/a/1.MP3";long startTime = System.currentTimeMillis();br.readWrite(from,"d:/b/2.MP3");long endTime = System.currentTimeMillis();System.out.println("直接讀取耗時:" + (endTime - startTime) +"ms");long startTime1 = System.currentTimeMillis();br.readWriteWithBuffer(from, "d:/b/3.MP3");long endTime1 = System.currentTimeMillis();System.out.println("使用緩衝區讀取耗時:" + (endTime1 - startTime1) +"ms");}/*************************************************************************** * 直接讀取檔案 *  * @param from * @param to * @throws IOException */public static void readWrite(String from, String to) throws IOException {InputStream in = null;OutputStream out = null;try {in = new FileInputStream(from);out = new FileOutputStream(to);while (true) {int data = in.read();if (data == -1) {break;}out.write(data);}} finally {if (in != null) {in.close();}if (out != null) {out.close();}}}/*************************************************************************** * 使用緩衝區讀寫檔案 * @param from * @param to * @throws IOException */public static void readWriteWithBuffer(String from, String to)throws IOException {InputStream inBuffer = null;OutputStream outBuffer = null;try {inBuffer = new BufferedInputStream(new FileInputStream(from));outBuffer = new BufferedOutputStream(new FileOutputStream(to));while (true) {int data = inBuffer.read();if (data == -1) {break;}outBuffer.write(data);}} finally {if (inBuffer != null) {inBuffer.close();}if (outBuffer != null) {outBuffer.close();}}}}

  

結果:對於5.8M大小的檔案,直接讀寫耗時:39266ms;使用緩衝區讀寫耗時:719ms。可見通過Buffer讀寫檔案效能的明顯優於直接讀寫。

    Java中專門提供提高I/O效率的緩衝類,這好比在資料讀寫時提供一個臨時緩衝區,每次讀取一個緩衝區大小的資料,將這資料庫一次性寫入目標裝置。中分別為兩種讀取方式。

 


    舉個簡單例子,在A地有10000本書需要搬到B地,如果一次搬1本,需要10000次。如果每次取1000本放到一個貨車上,運到B地,需要10次完成。貨車相當於是緩衝區。同樣道理,開設一個資料緩衝區每次讀取一資料區塊對於提高讀取效率有顯著提升。下面用一個具體程式碼範例來表示二者的效能差別。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.