内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

Java中常見的IO讀寫效率對比

這篇文章主要介紹“Java中常見的IO讀寫效率對比”,在日常操作中,相信很多人在Java中常見的IO讀寫效率對比問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java中常見的IO讀寫效率對比”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

在網(wǎng)站設(shè)計、成都做網(wǎng)站中從網(wǎng)站色彩、結(jié)構(gòu)布局、欄目設(shè)置、關(guān)鍵詞群組等細(xì)微處著手,突出企業(yè)的產(chǎn)品/服務(wù)/品牌,幫助企業(yè)鎖定精準(zhǔn)用戶,提高在線咨詢和轉(zhuǎn)化,使成都網(wǎng)站營銷成為有效果、有回報的無錫營銷推廣。創(chuàng)新互聯(lián)專業(yè)成都網(wǎng)站建設(shè)10年了,客戶滿意度97.8%,歡迎成都創(chuàng)新互聯(lián)客戶聯(lián)系。

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

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

package com.eric.io;   import java.io.BufferedInputStream;  import java.io.BufferedOutputStream;  import java.io.BufferedReader;  import java.io.BufferedWriter;  import java.io.ByteArrayInputStream;  import java.io.DataInputStream;  import java.io.DataOutputStream;  import java.io.File;  import java.io.FileInputStream;  import java.io.FileOutputStream;  import java.io.FileReader;  import java.io.FileWriter;  import java.io.IOException;  import java.io.InputStream;  import java.nio.ByteBuffer;  import java.nio.CharBuffer;  import java.nio.channels.FileChannel;   import com.eric.reflect.ExecuteTimerHandler;   public class ReadFileTools implements IReadFileTools {            /**       *        * execute readByBufferReader spend 444 million sencond!          execute readByBufferedInputStreamNoArray spend 27903 million sencond!          execute readByBufferedInputStream spend 192 million sencond!          execute readByChannel spend 484 million sencond!          execute readByChannelMap spend 42 million sencond!          execute readByDataInputStream spend 440 million sencond!       *        * @param args       * @throws Exception       */     public static final int     BUFFSIZE     = 180;      public static final String  root         = "E:\\sourcecode\\corejava\\src\\com\\eric\\io\\";      public static final boolean printContext    = false;            public static void main(String[] args) throws Exception {          String file = root + "VISA_INPUT_FULL";          IReadFileTools bi = (IReadFileTools) ExecuteTimerHandler.newInstance(new ReadFileTools());          bi.readByBufferReader(file);          bi.readByBufferedInputStreamNoArray(file);          bi.readByBufferedInputStream(file);          bi.readByChannel(file);          bi.readByChannelMap(file);          bi.readByDataInputStream(file);      }            /*       * execute readBuffer spend 421 million sencond! execute readByte spend       * 36172 million sencond!       */     public String readByBufferReader(String file) {          StringBuilder sb = new StringBuilder();          try {              BufferedReader br = new BufferedReader(new FileReader(new File(file)));              String line;              long count = 0;              while ((line = br.readLine()) != null) {                  if (printContext) {                      System.out.println(line);                  }                                    sb.append(line);                  count += line.length();              }              br.close();          } catch (Exception ex) {              ex.printStackTrace();          }          return sb.toString();      }            public void readByDataInputStream(String file) throws Exception {                    DataInputStream dis = new DataInputStream(new ByteArrayInputStream(new ReadFileTools().readByBufferReader(file).getBytes()));          while (dis.available() > 0) {              char c = (char) dis.read();              if (printContext) {                  System.out.println(c);              }          }      }      //this method not use byte array to get byte      public String readByBufferedInputStreamNoArray(String file) {          try {              InputStream is = new BufferedInputStream(new FileInputStream(new File(file)));              while (is.available() > 0) {                  char c = (char) is.read();                  if (printContext) {                      System.out.println(c);                  }              }          } catch (Exception ex) {              ex.printStackTrace();          }          return null;      }      //use byte array to get bytes from file      public void readByBufferedInputStream(String file) throws Exception {          BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));          byte[] bytes = new byte [BUFFSIZE];          while (input.available() > 0) {              input.read(bytes);          }      }      //use file channel to get byte from file      public void readByChannel(String file) throws Exception {                    FileChannel in = new FileInputStream(file).getChannel();          ByteBuffer buffer = ByteBuffer.allocate(BUFFSIZE);          while (in.read(buffer) != -1) {              buffer.flip(); // Prepare for writing              if (printContext) {                  System.out.println(buffer.getChar());              }              buffer.clear(); // Prepare for reading          }          in.close();      }      //use MappedByteBuffer to read byte from file      public void readByChannelMap(String file) throws Exception {          FileChannel fc = new FileInputStream(new File(file)).getChannel();          CharBuffer cb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).asCharBuffer();          char c;          while (cb.hasRemaining())              c = cb.get();          if (printContext) {              System.out.println(c);          }          fc.close();      }            public void copyFileByChannel(String file, String file2) throws Exception {                    FileChannel in = new FileInputStream(file).getChannel();          FileChannel out = new FileOutputStream(file2).getChannel();          ByteBuffer buffer = ByteBuffer.allocate(BUFFSIZE);          while (in.read(buffer) != -1) {              buffer.flip(); // Prepare for writing              out.write(buffer);              buffer.clear(); // Prepare for reading          }      }            public void test() {          System.out.println("test");      }            public void copyFile(String source, String dest) throws Exception {          BufferedReader br = new BufferedReader(new FileReader(new File(source)));          BufferedWriter bw = new BufferedWriter(new FileWriter(new File(dest)));          String temp;          while ((temp = br.readLine()) != null) {              bw.write(temp + "\n");          }      }            public void storingAndRecoveringData(String file) throws Exception {          DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));          DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));          dos.writeBoolean(false);          dos.writeByte(10);          dos.writeDouble(1213654);          dos.writeUTF("aihua");          dos.close();          System.out.println(dis.readBoolean());          System.out.println(dis.readByte());          System.out.println(dis.readDouble());          System.out.println(dis.readUTF());          dis.close();                }            public void doCopyFile(String src, String dest) throws IOException {          File srcFile = new File(src);          File destFile = new File(dest);          if (destFile.exists()) {              boolean d = destFile.delete();                            if (d) {                  System.out.print("刪除成功!");              } else {                  System.out.print("刪除失敗!");              }          }          BufferedInputStream input = new BufferedInputStream(new FileInputStream(srcFile));          try {              BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(destFile));              try {                  byte[] buffer = new byte [4096];                  int n = 0;                  while (-1 != (n = input.read(buffer))) {                      output.write(buffer, 0, n);                  }                  System.out.println("Copy Successful::" + dest);              } finally {                  try {                      if (output != null) {                          output.close();                      }                  } catch (IOException ioe) {                      ioe.printStackTrace();                  }              }          } finally {              try {                  if (input != null) {                      input.close();                  }              } catch (IOException ioe) {                  System.out.println("failed src file:" + src + " reason:" + ioe.getMessage());              }          }      }        }   /*   *    * History:   *    *    *    * $Log: $   */

到此,關(guān)于“Java中常見的IO讀寫效率對比”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

分享標(biāo)題:Java中常見的IO讀寫效率對比
本文地址:http://www.rwnh.cn/article0/gpoiio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)關(guān)鍵詞優(yōu)化、網(wǎng)頁設(shè)計公司小程序開發(fā)、Google定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)
东平县| 凤城市| 宁明县| 平邑县| 扶风县| 沙湾县| 连州市| 陆丰市| 澜沧| 黑河市| 望谟县| 东阿县| 商城县| 高淳县| 龙泉市| 土默特左旗| 雅江县| 武平县| 宁国市| 安多县| 上栗县| 南江县| 南康市| 巴林左旗| 左贡县| 眉山市| 共和县| 花莲市| 克山县| 萝北县| 漯河市| 兴业县| 库伦旗| 黄山市| 烟台市| 乳山市| 和平区| 微山县| 平泉县| 江达县| 吕梁市|