common.io.FileUtils大資料輸出報錯
阿新 • • 發佈:2021-06-06
FileUtils.writeStringToFile(file, content, DEFAULT_ENCODING);
FileUtils只提供了writeLines和writeStringtoFile方法
在輸出大資料時,content.getBytes會引起記憶體溢位
FileOutputStream fos = new FileOutputStream(file);
bw = new BufferedWriter(new OutputStreamWriter(fos,DEFAULT_ENCODING));
int length = content.length();
int start = 0;
while(length > 10000){
String str = new String(content.substring(start*10000, (start+1)*10000));
bw.write(str);
start++;
length -= 10000;
}
if(length > 0){
String str = new String(content.substring(start*10000));
bw.write(str);
}
bw.close();
轉載於:https://my.oschina.net/roywang/blog/133884