如题,先调用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.");
}

解决方案 »

  1.   

    FileReaderUtils.writeToFile(lines, f);调用这个方法又会创建一次f了。
      

  2.   

    在你贴的代码中没看到System.exit(0)
      

  3.   


    之前是这样的,也还是不行: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.");
    }
      

  4.   


    我其他地方会调用System.exit(0);就是一个关闭程序,我点了执行了System.exit(0);刚生成的文件就被删除了。只要不执行到System.exit(0);那个文件就一直在的。
      

  5.   

    不知道你System.exit(0);放在哪里,System.exit(0);是清除整个jvm里的内容,如果在你文件flush或者close前使用System.exit(0);应该会出现你这个情况的。System.exit(0);放到finally里或者最后面。
      

  6.   

    去看看创建文件的API吧,有很多种的,你这创建的是临时文件。也有可能是file没有close都试下
      

  7.   

    我都是等程序执行到System.out.println("writeToFile function finished.");这里结束了,然后点了一个按钮。按钮绑定的事件就是System.exit(0);
    只要我不点那个按钮,文件内容都也写好了,而且文件也一直在。太奇怪了。
      

  8.   


    File API就那么点,我都试了下。感觉是和流没正常关闭有关,我再仔细看看
      

  9.   

    一定要用System.exit(0)来结束程序么?
      

  10.   

    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);
             
        }
    System.exit(0)你应该写在这个类里面吧,你写在了这里:
    我都是等程序执行到System.out.println("writeToFile function finished.");这里结束了,然后点了一个按钮。按钮绑定的事件就是System.exit(0);是写在函数里面。调用的时候程序已经退出了
      

  11.   

    是不是这个程序的System.exit的时候调用了文件删除函数....或者是某个信号给处理了!
      

  12.   

    你程序有删除文件的地方吗?
    比如判断是否存在,如果存在的话就删除了,然后再重新创建。
    这里删除的时候要注意使用onDelete,不要使用onExistDelete。具体可以看API。
      

  13.   

    原因找到了,就是onExistDelete的问题。