我必须转储600万个文件,其中包含大约100-200个字符,而且速度很慢.实际的慢速部分是文件写入,如果我评论该部分(对WriteSoveraFile方法的调用)整个事情在5-10分钟内运行.事实上,我在一夜之间(16小时)运行并完成了200万条记录.
>有更快的方法吗?
>我最好创建一个数组数组,然后立即将它们全部转储? (我的系统只有4 GB,不会死于此消耗的6 GB数据?)
这是程序:
public static void WriteSoveraFile(String fileName, String path, String contents) throws IOException {
BufferedWriter bw = null;
try {
String outputFolderPath = cloGetAsFile( GenCCD.o_OutER7Folder ).getAbsolutePath() ;
File folder = new File( String.format("%1$s/Sovera/%2$s/", outputFolderPath, path) );
if (! folder.exists()) {
folder.mkdirs();
/* if (this.rcmdWriter != null)
this.rcmdWriter.close();
*/
}
File file = new File( String.format("%1$s/%2$s", folder.getAbsolutePath(),fileName) );
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
FileWriter fw = new FileWriter(file.getAbsoluteFile());
bw = new BufferedWriter(fw);
bw.write(contents);
bw.close();
}
/* else {
file.delete(); // want to delete the file?? or just overwrite it??
file.createNewFile();*/
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null) bw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}