Java中常见IO的读写效率对比

开发 后端
Java中的IO的类库非常的庞大,选择性非常的多,当面临一个问题时,往往不知道如何下手!下面的例子是对常见几种读文件方式的效率比较,通过一个动态代理的模式来统计每个方法的执行时间,测试文件是100多兆的数据文件。

Java中的IO的类库非常的庞大,选择性非常的多,当面临一个问题时,往往不知道如何下手!

更具我现在的理解,在效率不是非常重要的情况下,一般情况下可能只需要考虑两种情况,即想按照字节去读取,还是想按照行去读取,而一般情况无论采取什么方式去读取,***的方式都莫过于用Buffered...去包装要是用的类,而如果效率要求比较高则可以考虑是用FileChannel 或者是 file map,其中file Map是读写效率***的一种方式,如果读取的文件非常的大这种方式是***,下面的例子是对常见几种读文件方式的效率比较,通过一个动态代理的模式来统计每个方法的执行时间,测试文件是100多兆的数据文件。

  1. package com.eric.io;  
  2.  
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.BufferedReader;  
  6. import java.io.BufferedWriter;  
  7. import java.io.ByteArrayInputStream;  
  8. import java.io.DataInputStream;  
  9. import java.io.DataOutputStream;  
  10. import java.io.File;  
  11. import java.io.FileInputStream;  
  12. import java.io.FileOutputStream;  
  13. import java.io.FileReader;  
  14. import java.io.FileWriter;  
  15. import java.io.IOException;  
  16. import java.io.InputStream;  
  17. import java.nio.ByteBuffer;  
  18. import java.nio.CharBuffer;  
  19. import java.nio.channels.FileChannel;  
  20.  
  21. import com.eric.reflect.ExecuteTimerHandler;  
  22.  
  23. public class ReadFileTools implements IReadFileTools {  
  24.       
  25.     /**  
  26.      *   
  27.      * execute readByBufferReader spend 444 million sencond!  
  28.         execute readByBufferedInputStreamNoArray spend 27903 million sencond!  
  29.         execute readByBufferedInputStream spend 192 million sencond!  
  30.         execute readByChannel spend 484 million sencond!  
  31.         execute readByChannelMap spend 42 million sencond!  
  32.         execute readByDataInputStream spend 440 million sencond!  
  33.      *   
  34.      * @param args  
  35.      * @throws Exception  
  36.      */ 
  37.     public static final int     BUFFSIZE     = 180;  
  38.     public static final String  root         = "E:\\sourcecode\\corejava\\src\\com\\eric\\io\\";  
  39.     public static final boolean printContext    = false;  
  40.       
  41.     public static void main(String[] args) throws Exception {  
  42.         String file = root + "VISA_INPUT_FULL";  
  43.         IReadFileTools bi = (IReadFileTools) ExecuteTimerHandler.newInstance(new ReadFileTools());  
  44.         bi.readByBufferReader(file);  
  45.         bi.readByBufferedInputStreamNoArray(file);  
  46.         bi.readByBufferedInputStream(file);  
  47.         bi.readByChannel(file);  
  48.         bi.readByChannelMap(file);  
  49.         bi.readByDataInputStream(file);  
  50.     }  
  51.       
  52.     /*  
  53.      * execute readBuffer spend 421 million sencond! execute readByte spend  
  54.      * 36172 million sencond!  
  55.      */ 
  56.     public String readByBufferReader(String file) {  
  57.         StringBuilder sb = new StringBuilder();  
  58.         try {  
  59.             BufferedReader br = new BufferedReader(new FileReader(new File(file)));  
  60.             String line;  
  61.             long count = 0;  
  62.             while ((line = br.readLine()) != null) {  
  63.                 if (printContext) {  
  64.                     System.out.println(line);  
  65.                 }  
  66.                   
  67.                 sb.append(line);  
  68.                 count += line.length();  
  69.             }  
  70.             br.close();  
  71.         } catch (Exception ex) {  
  72.             ex.printStackTrace();  
  73.         }  
  74.         return sb.toString();  
  75.     }  
  76.       
  77.     public void readByDataInputStream(String file) throws Exception {  
  78.           
  79.         DataInputStream dis = new DataInputStream(new ByteArrayInputStream(new ReadFileTools().readByBufferReader(file).getBytes()));  
  80.         while (dis.available() > 0) {  
  81.             char c = (char) dis.read();  
  82.             if (printContext) {  
  83.                 System.out.println(c);  
  84.             }  
  85.         }  
  86.     }  
  87.     //this method not use byte array to get byte  
  88.     public String readByBufferedInputStreamNoArray(String file) {  
  89.         try {  
  90.             InputStream is = new BufferedInputStream(new FileInputStream(new File(file)));  
  91.             while (is.available() > 0) {  
  92.                 char c = (char) is.read();  
  93.                 if (printContext) {  
  94.                     System.out.println(c);  
  95.                 }  
  96.             }  
  97.         } catch (Exception ex) {  
  98.             ex.printStackTrace();  
  99.         }  
  100.         return null;  
  101.     }  
  102.     //use byte array to get bytes from file  
  103.     public void readByBufferedInputStream(String file) throws Exception {  
  104.         BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));  
  105.         byte[] bytes = new byte [BUFFSIZE];  
  106.         while (input.available() > 0) {  
  107.             input.read(bytes);  
  108.         }  
  109.     }  
  110.     //use file channel to get byte from file  
  111.     public void readByChannel(String file) throws Exception {  
  112.           
  113.         FileChannel in = new FileInputStream(file).getChannel();  
  114.         ByteBuffer buffer = ByteBuffer.allocate(BUFFSIZE);  
  115.         while (in.read(buffer) != -1) {  
  116.             buffer.flip(); // Prepare for writing  
  117.             if (printContext) {  
  118.                 System.out.println(buffer.getChar());  
  119.             }  
  120.             buffer.clear(); // Prepare for reading  
  121.         }  
  122.         in.close();  
  123.     }  
  124.     //use MappedByteBuffer to read byte from file  
  125.     public void readByChannelMap(String file) throws Exception {  
  126.         FileChannel fc = new FileInputStream(new File(file)).getChannel();  
  127.         CharBuffer cb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).asCharBuffer();  
  128.         char c;  
  129.         while (cb.hasRemaining())  
  130.             c = cb.get();  
  131.         if (printContext) {  
  132.             System.out.println(c);  
  133.         }  
  134.         fc.close();  
  135.     }  
  136.       
  137.     public void copyFileByChannel(String file, String file2) throws Exception {  
  138.           
  139.         FileChannel in = new FileInputStream(file).getChannel();  
  140.         FileChannel out = new FileOutputStream(file2).getChannel();  
  141.         ByteBuffer buffer = ByteBuffer.allocate(BUFFSIZE);  
  142.         while (in.read(buffer) != -1) {  
  143.             buffer.flip(); // Prepare for writing  
  144.             out.write(buffer);  
  145.             buffer.clear(); // Prepare for reading  
  146.         }  
  147.     }  
  148.       
  149.     public void test() {  
  150.         System.out.println("test");  
  151.     }  
  152.       
  153.     public void copyFile(String source, String dest) throws Exception {  
  154.         BufferedReader br = new BufferedReader(new FileReader(new File(source)));  
  155.         BufferedWriter bw = new BufferedWriter(new FileWriter(new File(dest)));  
  156.         String temp;  
  157.         while ((temp = br.readLine()) != null) {  
  158.             bw.write(temp + "\n");  
  159.         }  
  160.     }  
  161.       
  162.     public void storingAndRecoveringData(String file) throws Exception {  
  163.         DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));  
  164.         DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));  
  165.         dos.writeBoolean(false);  
  166.         dos.writeByte(10);  
  167.         dos.writeDouble(1213654);  
  168.         dos.writeUTF("aihua");  
  169.         dos.close();  
  170.         System.out.println(dis.readBoolean());  
  171.         System.out.println(dis.readByte());  
  172.         System.out.println(dis.readDouble());  
  173.         System.out.println(dis.readUTF());  
  174.         dis.close();  
  175.           
  176.     }  
  177.       
  178.     public void doCopyFile(String src, String dest) throws IOException {  
  179.         File srcFile = new File(src);  
  180.         File destFile = new File(dest);  
  181.         if (destFile.exists()) {  
  182.             boolean d = destFile.delete();  
  183.               
  184.             if (d) {  
  185.                 System.out.print("删除成功!");  
  186.             } else {  
  187.                 System.out.print("删除失败!");  
  188.             }  
  189.         }  
  190.         BufferedInputStream input = new BufferedInputStream(new FileInputStream(srcFile));  
  191.         try {  
  192.             BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(destFile));  
  193.             try {  
  194.                 byte[] buffer = new byte [4096];  
  195.                 int n = 0;  
  196.                 while (-1 != (n = input.read(buffer))) {  
  197.                     output.write(buffer, 0, n);  
  198.                 }  
  199.                 System.out.println("Copy Successful::" + dest);  
  200.             } finally {  
  201.                 try {  
  202.                     if (output != null) {  
  203.                         output.close();  
  204.                     }  
  205.                 } catch (IOException ioe) {  
  206.                     ioe.printStackTrace();  
  207.                 }  
  208.             }  
  209.         } finally {  
  210.             try {  
  211.                 if (input != null) {  
  212.                     input.close();  
  213.                 }  
  214.             } catch (IOException ioe) {  
  215.                 System.out.println("failed src file:" + src + " reason:" + ioe.getMessage());  
  216.             }  
  217.         }  
  218.     }  
  219.       
  220. }  
  221.  
  222. /*  
  223.  *   
  224.  * History:  
  225.  *   
  226.  *   
  227.  *   
  228.  * $Log: $  
  229.  */ 

希望前辈可以对方法进行补充。

原文链接:http://blog.csdn.net/sun7545526/article/details/7413347

【编辑推荐】

  1. 实战是硬道理:记Java技术面试
  2. Java中的Enum的使用与分析
  3. 按权重选取目标的Java算法
  4. 通用Java文件上传和下载组件的设计与实现
  5. 赶紧重写Java的时间和日期API吧!
责任编辑:林师授 来源: sun7545526的博客
相关推荐

2009-06-30 16:03:00

异常Java

2019-10-30 16:03:48

JavaJava虚拟机数据库

2024-02-19 16:23:11

2020-08-13 06:43:41

React前端开发

2014-12-23 09:47:34

2024-01-08 17:36:09

2009-03-10 09:46:00

ADSL协议

2020-07-10 17:40:01

人工智能网络技术

2019-06-21 10:13:26

JavaScript错误开发

2011-04-08 13:58:52

JavaJSP

2023-10-31 18:57:02

Java字符串

2022-07-20 12:24:38

Python列表集合

2009-08-27 11:12:04

C# foreach

2019-03-21 14:18:38

iOS开发优化原因

2019-04-09 21:10:23

iOS加密框架

2020-05-29 09:36:59

越权访问漏洞Web安全

2022-09-26 00:00:00

神经网络激活函数sigmoid

2012-08-22 10:44:08

软件开发

2013-06-04 13:38:27

2022-11-15 21:21:06

Linux中国
点赞
收藏

51CTO技术栈公众号