日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Java中常見IO的讀寫效率對比

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

修水ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!

更具我現(xiàn)在的理解,在效率不是非常重要的情況下,一般情況下可能只需要考慮兩種情況,即想按照字節(jié)去讀取,還是想按照行去讀取,而一般情況無論采取什么方式去讀取,***的方式都莫過于用Buffered...去包裝要是用的類,而如果效率要求比較高則可以考慮是用FileChannel 或者是 file map,其中file Map是讀寫效率***的一種方式,如果讀取的文件非常的大這種方式是***,下面的例子是對常見幾種讀文件方式的效率比較,通過一個(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("刪除失敗!");  
  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.  */ 

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


當(dāng)前題目:Java中常見IO的讀寫效率對比
網(wǎng)頁地址:http://www.5511xx.com/article/cdodiip.html