代码改变世界

java知识点8

2016-08-13 16:26  威震天1606  阅读(140)  评论(0编辑  收藏  举报

 我们知道Java中一般的输入输出流类都是用单字节的读取方法来进行I/O操作的,也就是说每次只读写一个字节的数据,这种方法显然繁琐低效。如果从设备读取10M的文件,每次读取一个字节,完成操作将需要做10M/次I/O操作,I/O操作又是一件相当耗时的事情,无疑在很大程度上降低了系统的性能。

 


    Java中专门提供提高I/O效率的缓冲类,这好比在数据读写时提供一个临时缓冲区,每次读取一个缓冲区大小的数据,将这数据库一次性写入目标设备。下图中分别为两种读取方式。

 


    举个简单例子,在A地有10000本书需要搬到B地,如果一次搬1本,需要10000次。如果每次取1000本放到一个货车上,运到B地,需要10次完成。货车相当于是缓存区。同样道理,开设一个数据缓存区每次读取一数据块对于提高读取效率有显著提升。下面用一个具体代码示例来表示二者的性能差别。

 

 

Java代码  收藏代码
    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. }