[問題] 合成長字串時出現OutOfMemoryError
我的程式目的是想將文字檔變成一行字串 (StringBuffer的物件)
每個檔案的大小約40 mb,
因為第一行的標題不想串入StringBuffer的物件內,
所以想跳掉第一行,
但不知道為什麼少串這行,在執行時反而會發生錯誤。
(若拿掉String str = input.readLine()就可以)
import java.io.*;
public class Test{
public static void main(String[] args) throws Exception {
String file = args[0];
BufferedReader input0 =
new BufferedReader(new FileReader(new File(file)));
//放欲開啟檔案的檔名 第一行1.txt 第二行2.txt...
String str0;
while ((str0= input0.readLine()) != null){
BufferedReader input =
new BufferedReader(new FileReader(new File(str0)));
String str = input.readLine();
原本想利用這行來跳過標題列
但多這一行就會有錯誤 OutOfMemoryError: Java heap space
StringBuffer sb = new StringBuffer();
while ((str = input.readLine()) != null){
sb = sb.append(str);
問題顯示是出現在這
}
System.out.println(sb.length());
input.close();
}
input0.close();
}
}
請板上有經驗的板友前輩不吝賜教
非常感謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.25.239.129
※ 編輯: ntb 來自: 114.25.239.129 (06/28 00:24)
→
06/28 17:11, , 1F
06/28 17:11, 1F
→
06/29 13:35, , 2F
06/29 13:35, 2F
→
06/29 14:11, , 3F
06/29 14:11, 3F
→
06/29 14:49, , 4F
06/29 14:49, 4F
討論串 (同標題文章)
以下文章回應了本文 (最舊先):
完整討論串 (本文為第 1 之 4 篇):