成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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-06-21 10:13:26

JavaScript錯誤開發

2020-07-10 17:40:01

人工智能網絡技術

2014-12-23 09:47:34

2020-08-13 06:43:41

React前端開發

2024-01-08 17:36:09

2009-03-10 09:46:00

ADSL協議

2019-10-30 16:03:48

JavaJava虛擬機數據庫

2024-02-19 16:23:11

2013-06-04 13:38:27

2012-08-22 10:44:08

軟件開發

2022-11-15 21:21:06

Linux中國

2022-03-17 08:34:47

TypeScript項目類型

2009-08-27 11:12:04

C# foreach

2019-03-21 14:18:38

iOS開發優化原因

2022-09-26 00:00:00

神經網絡激活函數sigmoid

2020-05-29 09:36:59

越權訪問漏洞Web安全

2019-04-09 21:10:23

iOS加密框架

2017-11-22 14:20:07

前端JavaScript排序算法

2011-04-08 13:58:52

JavaJSP
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 99re6在线视频精品免费 | 精精国产xxxx视频在线播放 | 国产成人精品一区二 | 午夜资源 | 国产日韩精品视频 | 久久黄视频 | 中文字幕免费在线观看 | 日韩精品视频在线观看一区二区三区 | 久久国产精品视频 | 国产日韩精品一区 | 亚洲欧美第一视频 | 中文字幕精品一区二区三区精品 | 成人精品国产免费网站 | 亚洲一二三区在线观看 | 日本在线中文 | 国产做a爱片久久毛片 | 一级黄色片免费在线观看 | 亚洲h在线观看 | 老外黄色一级片 | 欧美激情在线播放 | 国产精品久久久久久久久久 | 日韩欧美在线观看一区 | 午夜av成人| 国产精品综合色区在线观看 | 午夜精品一区二区三区在线观看 | 久久综合狠狠综合久久综合88 | 精品久久久久久久 | 午夜精品一区二区三区在线视频 | 91视频a| 国产精品毛片久久久久久 | 日韩精品在线看 | 欧美在线视频一区二区 | 免费观看黄色片视频 | 日韩视频在线播放 | 毛片99 | 不卡av在线 | 女女百合av大片一区二区三区九县 | 午夜视频网站 | 久久久久国产精品人 | 精品av | 污片在线观看 |