[英]Write string to huge file
我知道有關於這個問題的幾個主題,但我認為我的問題因為大小而有點不同。
在我的例子中,我想將1,700萬行寫入文本文件。 在最壞的情況下,可能會有更多。 這些行是為sql加載器創建的,用於將快速數據加載到表中,因此文件可能非常大,因為sql loader可以處理它。
現在我想盡快寫出大文件。 這是我的實際方法:
BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt"),40000);
int u=profils.size()-1;
for(int z=0; z<u;z++){
for(int b=0;b<z;b++){
p = getValue();
if(!Double.isNaN(p) & p > 0.55){
bw.write(map.get(z) + ";" + map.get(b) + ";" + p + "\n");
}
}
}
bw.close();
對於我的1,700萬行,我需要大約20分鍾。 我可以用任何我不知道的方法更快地處理它嗎?
FileChannel:
File out = new File("out.txt");
FileOutputStream fileOutputStream = new FileOutputStream(out, true);
FileChannel fileChannel = fileOutputStream.getChannel();
ByteBuffer byteBuffer = null;
int u=profils.size()-1;
for(int z=0; z<u;z++){
for(int b=0;b<z;b++){
p = getValue();
if(!Double.isNaN(p) & p > 0.55){
String str = indexToSubstID.get(z) + ";" + indexToSubstID.get(b) + ";" + p + "\n";
byteBuffer = ByteBuffer.wrap(str.getBytes(Charset.forName("ISO-8859-1")));
fileChannel.write(byteBuffer);
}
}
}
fileOutputStream.close();
FileChannel是你的方式。 它用於大量寫入。 在這里閱讀api文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.