[問題] 關於讀大文字檔的OutOfMemoryError

看板java作者 (還沒想到)時間12年前 (2011/07/21 16:25), 編輯推噓0(007)
留言7則, 3人參與, 最新討論串1/1
大家好~ 小弟因為打工需要 算是剛接觸JAVA不久 基本上都是一直參考CODE來學習 最近要寫一個處理很大純文字檔的tool 檔案大約快200Mb 目前能做到的 單純read的話大約能讀70~80M 想說之後seperate剪接處理應該能做到200M 不過用swing做出來之後 大約15M的檔案就會出現 OutOfMemoryError 有爬過文 用-Xmx去改JVM default設置可以成功 可是如果這個tool 用exej4轉exe後 給其他台電腦用 是不是也要去改呢? 所以想改一下演算法 不過不知道該從哪部分下手 可否請各位大大幫忙看一下 拜託了 public static void read2array(File file) throws Exception { InputStream in = null; long t_i,t_f = 0; try{ in = new BufferedInputStream(new FileInputStream(file)); int bufLen = 20000*1024; byte[] buf = new byte[bufLen]; byte[] tmp = null; int len = 0; String strFileContents; t_i = System.currentTimeMillis(); while((len = in.read(buf,0,bufLen)) != -1){ // extend array tmp = new byte[out.length + len]; // copy data System.arraycopy(out,0,tmp,0,out.length); System.arraycopy(buf,0,tmp,out.length,len); out = tmp; tmp = null; strFileContents = new String(out, 0, len); textArea.append(strFileContents); } }finally{ // always close the stream if (in != null) try{ in.close(); t_f = System.currentTimeMillis()-t_i; System.out.println(t_f); }catch (Exception e){ JOptionPane.showMessageDialog(null, e.toString(), "開啟檔案失敗", JOptionPane.ERROR_MESSAGE); } } } ps上面是有參考有人寫的 bigfilereader -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 211.23.86.187

07/21 16:31, , 1F
你要做的處理是什麼?能不能讀一點做一點?
07/21 16:31, 1F

07/21 16:41, , 2F
嗯嗯 處理檔案是沒問題的 是要把還沒處理的檔案顯示在
07/21 16:41, 2F

07/21 16:43, , 3F
Jtextarea 區域時 才會出現 outofmemory error
07/21 16:43, 3F

07/21 18:22, , 4F
請自己做 paging(分頁) 不要一次全載入
07/21 18:22, 4F

07/21 21:07, , 5F
好~ 謝謝D大 我會試試看~
07/21 21:07, 5F

07/25 14:58, , 6F
我有試著做paging 不還是會出現 outofMemoryerror QQ
07/25 14:58, 6F

07/25 15:14, , 7F
先貼 code 上來看一下你 paging + JTextArea 怎麼寫的
07/25 15:14, 7F
文章代碼(AID): #1E9-9xqE (java)