<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          JAVA讀取很大的TXT文件(上百G)

          共 1409字,需瀏覽 3分鐘

           ·

          2020-12-28 03:29

          點擊上方藍色字體,選擇“標(biāo)星公眾號”

          優(yōu)質(zhì)文章,第一時間送達

          ? 作者?|??UnmatchedSelf

          來源 |? urlify.cn/NNBN7n

          66套java從入門到精通實戰(zhàn)課程分享

          當(dāng)文件過于大的時候我們?nèi)绻€用傳統(tǒng)的方式讀取很容易造成內(nèi)存滿我們應(yīng)該拆開讀?。?/p>

          用NIO方式把大文件分成固定大小的小文件(小于2G,自己綜合分析設(shè)置多大,每個文件太大容易浪費空間,太小浪費時間),然后再循環(huán)用方案1去讀每個小文件。

          ?

          public?static?void?readLargeTextWithNIO()?{
          ??????try?{
          ??????????long?startTime?=?System.currentTimeMillis();
          ??????????//?要讀取的文件
          ??????????FileInputStream?fin?=?new?FileInputStream("/Applications/demo/aaaa.txt");
          ??????????FileChannel?fcin?=?fin.getChannel();
          ??????????//?文件過大拆分成?128M?大小的txt文件?
          ??????????ByteBuffer?buffer?=?ByteBuffer.allocate(128?*?1024?*?1024);
          ??????????while?(true)?{
          ??????????????buffer.clear();
          ??????????????int?flag?=?fcin.read(buffer);
          ??????????????if?(flag?==?-1)?{
          ??????????????????break;
          ??????????????}
          ??????????????buffer.flip();
          ??????????????//?輸出到指定的位置?
          ??????????????FileOutputStream?fileInputStream?=?new?FileOutputStream("/Applications/demo/"+?UUID.randomUUID().toString()+".txt");
          ??????????????FileChannel?channel?=?fileInputStream.getChannel();
          ??????????????channel.write(buffer);
          ??????????}
          ??????????long?endTime?=?System.currentTimeMillis();
          ??????????System.out.println("?共消耗:"?+?(endTime-startTime)?/?1000?+"秒");
          ??????????//?分割成了?每一個文件?大小是??128M,?然后在用傳統(tǒng)的方法去讀取即可
          ??????}?catch?(Exception?e)?{

          ??????}
          }
          ?





          粉絲福利:Java從入門到入土學(xué)習(xí)路線圖

          ???

          ?長按上方微信二維碼?2 秒


          感謝點贊支持下哈?

          瀏覽 44
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日本在线视频精品 | 亚洲在线观看免费视频 | 天天射天天操天天透人妻 | 五月丁香啪啪 | 日本黄色片网站一二 |