[問題] 關於讀大文字檔的OutOfMemoryError
大家好~
小弟因為打工需要
算是剛接觸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
07/21 16:43, 3F
→
07/21 18:22, , 4F
07/21 18:22, 4F
→
07/21 21:07, , 5F
07/21 21:07, 5F
→
07/25 14:58, , 6F
07/25 14:58, 6F
→
07/25 15:14, , 7F
07/25 15:14, 7F