我們知道Java中一般的輸入輸出資料流類都是用單位元組的讀取方法來進行I/O操作的,也就是說每次唯讀寫一個位元組的資料,這種方法顯然繁瑣低效。如果從裝置讀取10M的檔案,每次讀取一個位元組,完成操作將需要做10M/次I/O操作,I/O操作又是一件相當耗時的事情,無疑在很大程度上降低了系統的效能。
Java代碼 {
dp.sh.Toolbar.CopyToClipboard(this);return false;
}" href="http://writeblog.csdn.net/#">
- 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();
- }
- }
- }
-
- }
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次完成。貨車相當於是緩衝區。同樣道理,開設一個資料緩衝區每次讀取一資料區塊對於提高讀取效率有顯著提升。下面用一個具體程式碼範例來表示二者的效能差別。