日期:2014-05-20  浏览次数:20661 次

FileWriter 写完文件,用System.exit(0);文件就被删除了
如题,先调用writeToIniFile,其中先删除这个文件,再重新写。问题是只要一执行到System.exit(0)刚写的文件就被删除了,什么原因?


public void writeToIniFile() throws IOException {
System.out.println("start to write info into news.ini");

String newsIni = USBConfig.drivePath + USBConfig.INIT_NEW_FOLDER + "\\news.ini";
File f = new File(newsIni);
if(f.exists()){
f.delete();
}
f.createNewFile();
int sum = this.getnewsList().size();
List<String> lines = new ArrayList<String>();
lines.add("[news]");
lines.add("sum="+sum);
StringBuilder line = new StringBuilder("news");
for(int i=0; i < sum; i++){
News n = this.getnewsList().get(i);
line.append(i).append("=").append(n.content);
lines.add(line.toString());
line = new StringBuilder("news");
}
System.out.println("the lines in memory ===============");
for(String s : lines){
System.out.println(s);
}
System.out.println("the lines in memory ===============");

FileReaderUtils.writeToFile(lines, f);

}



public static void writeToFile(List<String> lines, File ff) throws IOException{
System.out.println("writeToFile function started.");
File f = new File(ff.getAbsolutePath());
f.createNewFile();
String encoding = code(f);
FileOutputStream fos = new FileOutputStream(f);
OutputStreamWriter writer = new OutputStreamWriter(fos,
encoding);
// FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(writer);
for(String s : lines){
// bw.append(s);
bw.write(s);
bw.newLine();
bw.flush();
}
bw.flush();
writer.flush();
fos.flush();
bw.close();
writer.close();
fos.close();
System.out.println("writeToFile function finished.");
}

------解决方案--------------------