自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java中常見IO的讀寫效率對(duì)比

開發(fā) 后端
Java中的IO的類庫非常的龐大,選擇性非常的多,當(dāng)面臨一個(gè)問題時(shí),往往不知道如何下手!下面的例子是對(duì)常見幾種讀文件方式的效率比較,通過一個(gè)動(dòng)態(tài)代理的模式來統(tǒng)計(jì)每個(gè)方法的執(zhí)行時(shí)間,測試文件是100多兆的數(shù)據(jù)文件。

Java中的IO的類庫非常的龐大,選擇性非常的多,當(dāng)面臨一個(gè)問題時(shí),往往不知道如何下手!

更具我現(xiàn)在的理解,在效率不是非常重要的情況下,一般情況下可能只需要考慮兩種情況,即想按照字節(jié)去讀取,還是想按照行去讀取,而一般情況無論采取什么方式去讀取,***的方式都莫過于用Buffered...去包裝要是用的類,而如果效率要求比較高則可以考慮是用FileChannel 或者是 file map,其中file Map是讀寫效率***的一種方式,如果讀取的文件非常的大這種方式是***,下面的例子是對(duì)常見幾種讀文件方式的效率比較,通過一個(gè)動(dòng)態(tài)代理的模式來統(tǒng)計(jì)每個(gè)方法的執(zhí)行時(shí)間,測試文件是100多兆的數(shù)據(jù)文件。

  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("刪除失?。?quot;);  
  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.  */ 

希望前輩可以對(duì)方法進(jìn)行補(bǔ)充。

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

【編輯推薦】

  1. 實(shí)戰(zhàn)是硬道理:記Java技術(shù)面試
  2. Java中的Enum的使用與分析
  3. 按權(quán)重選取目標(biāo)的Java算法
  4. 通用Java文件上傳和下載組件的設(shè)計(jì)與實(shí)現(xiàn)
  5. 趕緊重寫Java的時(shí)間和日期API吧!
責(zé)任編輯:林師授 來源: sun7545526的博客
相關(guān)推薦

2009-06-30 16:03:00

異常Java

2024-02-19 16:23:11

2019-10-30 16:03:48

JavaJava虛擬機(jī)數(shù)據(jù)庫

2019-06-21 10:13:26

JavaScript錯(cuò)誤開發(fā)

2020-07-10 17:40:01

人工智能網(wǎng)絡(luò)技術(shù)

2014-12-23 09:47:34

2020-08-13 06:43:41

React前端開發(fā)

2024-01-08 17:36:09

2009-03-10 09:46:00

ADSL協(xié)議

2012-08-22 10:44:08

軟件開發(fā)

2013-06-04 13:38:27

2022-03-17 08:34:47

TypeScript項(xiàng)目類型

2022-11-15 21:21:06

Linux中國

2023-10-31 18:57:02

Java字符串

2011-04-08 13:58:52

JavaJSP

2019-04-09 21:10:23

iOS加密框架

2009-08-27 11:12:04

C# foreach

2019-03-21 14:18:38

iOS開發(fā)優(yōu)化原因

2022-09-26 00:00:00

神經(jīng)網(wǎng)絡(luò)激活函數(shù)sigmoid

2020-05-29 09:36:59

越權(quán)訪問漏洞Web安全
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)