如题,先调用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.");
}
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.");
}
之前是这样的,也还是不行:public static void writeToFile(List<String> lines, File f) 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.");
}
我其他地方会调用System.exit(0);就是一个关闭程序,我点了执行了System.exit(0);刚生成的文件就被删除了。只要不执行到System.exit(0);那个文件就一直在的。
只要我不点那个按钮,文件内容都也写好了,而且文件也一直在。太奇怪了。
File API就那么点,我都试了下。感觉是和流没正常关闭有关,我再仔细看看
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);
}
System.exit(0)你应该写在这个类里面吧,你写在了这里:
我都是等程序执行到System.out.println("writeToFile function finished.");这里结束了,然后点了一个按钮。按钮绑定的事件就是System.exit(0);是写在函数里面。调用的时候程序已经退出了
比如判断是否存在,如果存在的话就删除了,然后再重新创建。
这里删除的时候要注意使用onDelete,不要使用onExistDelete。具体可以看API。