【问题标题】:Overwriting txt file in java在java中覆盖txt文件
【发布时间】:2012-11-23 15:44:55
【问题描述】:

我编写的代码应该覆盖所选文本文件的内容,但它正在附加它。我到底做错了什么?

File fnew=new File("../playlist/"+existingPlaylist.getText()+".txt");
String source = textArea.getText();
System.out.println(source);
FileWriter f2;

try {
    f2 = new FileWriter(fnew,false);
    f2.write(source);
    /*for (int i=0; i<source.length();i++)
    {
        if(source.charAt(i)=='\n')
            f2.append(System.getProperty("line.separator"));
        f2.append(source.charAt(i));
    }*/
    f2.close();
} catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}           

编辑

我尝试创建一个新的 temp.txt 文件并将新内容写入其中,删除此文本文件并将 temp.txt 重命名为该文件。问题是,删除总是不成功。我认为我不必为此更改用户权限吗?

另外,我的程序的一部分列出了这个目录中的所有文件,所以我猜它们正被程序使用,因此无法删除。但是为什么不覆盖呢?

已解决

我最大的“D'oh”时刻!我一直在 Eclipse 上编译它,而不是在我执行它的 cmd 上。所以我新编译的类进入 bin 文件夹,通过命令提示符编译的类文件在我的 src 文件夹中保持不变。我用我的新代码重新编译,它就像一个魅力。

File fold=new File("../playlist/"+existingPlaylist.getText()+".txt");
fold.delete();
File fnew=new File("../playlist/"+existingPlaylist.getText()+".txt");
String source = textArea.getText();
System.out.println(source);

try {
    FileWriter f2 = new FileWriter(fnew, false);
    f2.write(source);
    f2.close();
} catch (IOException e) {
    e.printStackTrace();
}           

【问题讨论】:

  • 我认为应该可以。它在我的情况下工作。
  • 它也适用于我的情况。可能是您的程序中发生了一些奇怪的事情。尝试解决该问题。首先删除整个内容,然后开始将新内容写入同一文件。
  • 关于如何删除内容的任何提示?
  • fnew.delete();这应该可以解决问题。
  • 试过了。将其设置为布尔变量。它总是错误的:\

标签: java overwrite filewriter


【解决方案1】:

您的代码对我来说很好用。它按预期替换了文件中的文本并且没有追加。

如果要追加,则在

中设置第二个参数
new FileWriter(fnew,false);

为真;

【讨论】:

    【解决方案2】:

    已解决

    我最大的“D'oh”时刻!我一直在 Eclipse 上编译它,而不是在我执行它的 cmd 上。所以我新编译的类进入了 bin 文件夹,通过命令提示符编译的类文件在我的 src 文件夹中保持不变。我用我的新代码重新编译,它就像一个魅力。

    File fold = new File("../playlist/" + existingPlaylist.getText() + ".txt");
    fold.delete();
    
    File fnew = new File("../playlist/" + existingPlaylist.getText() + ".txt");
    
    String source = textArea.getText();
    System.out.println(source);
    
    try {
        FileWriter f2 = new FileWriter(fnew, false);
        f2.write(source);
        f2.close();
    
    } catch (IOException e) {
        e.printStackTrace();
    }   
    

    【讨论】:

      【解决方案3】:

      初始化文件对象后再添加一行

      File fnew = new File("../playlist/" + existingPlaylist.getText() + ".txt");
      fnew.createNewFile();
      

      【讨论】:

      • new File 足以创建一个新文件
      • 我只是在设置现有文件的路径。
      • @ALJIMohamed 不,new File 只是创建代表文件抽象概念的 Java 对象。否则,使用exists() 方法将毫无意义,因为该文件将始终存在于new File 之后。
      • @Torcellite 是的,当然你是在设置路径,但是当你想覆盖时,创建新文件是一样的。
      • @AndroidKiller。使用false 作为FileWriter 的第二个参数将附加模式设置为false。 OP中的原始代码应该可以工作。不知道为什么它不起作用。它在我的情况下工作。
      【解决方案4】:

      这稍微简化了一点,它的行为就像你想要的那样。

      FileWriter f = new FileWriter("../playlist/"+existingPlaylist.getText()+".txt");
      
      try {
       f.write(source);
       ...
      } catch(...) {
      } finally {
       //close it here
      }
      

      【讨论】:

      • 是一样的。它仍在附加文本
      【解决方案5】:

      覆盖文本文件的最简单方法是使用公共静态字段。

      这每次都会覆盖文件,因为你只使用 false 第一次通过。`

      public static boolean appendFile;
      

      使用它只允许一次通过附加字段的写入序列 写代码为假。

      // use your field before processing the write code
      
      appendFile = False;
      
      File fnew=new File("../playlist/"+existingPlaylist.getText()+".txt");
      String source = textArea.getText();
      System.out.println(source);
      FileWriter f2;
      
      try {
           //change this line to read this
      
          // f2 = new FileWriter(fnew,false);
      
          // to read this
          f2 = new FileWriter(fnew,appendFile); // important part
          f2.write(source);
      
          // change field back to true so the rest of the new data will
          // append to the new file.
      
          appendFile = true;
      
          f2.close();
       } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
       }           
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-07
        • 1970-01-01
        • 2021-07-19
        • 2012-04-19
        • 1970-01-01
        • 2017-09-14
        • 2015-01-03
        • 1970-01-01
        相关资源
        最近更新 更多