简体   繁体   English

在java中覆盖txt文件

[英]Overwriting txt file in java

The code I've written is supposed to overwrite over the contents of the selected text file, but it's appending it.我编写的代码应该覆盖所选文本文件的内容,但它附加了它。 What am I doing wrong exactly?我到底做错了什么?

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();
}           

EDIT编辑

I tried making a new temp.txt file and writing the new contents into that, deleting this text file and renaming temp.txt to this one.我尝试创建一个新的 temp.txt 文件并将新内容写入其中,删除该文本文件并将 temp.txt 重命名为该文件。 Thing is, the deletion is always unsuccessful.事实是,删除总是不成功的。 I don't think I have to change user permissions for this do I?我认为我不必为此更改用户权限,对吗?

Also, a part of my program lists all the files in this directory, so I'm guessing they're being used by the program and so can't be deleted.此外,我的程序的一部分列出了此目录中的所有文件,所以我猜它们正在被程序使用,因此无法删除。 But why not overwritten?但为什么不覆盖?

SOLVED解决了

My biggest "D'oh" moment!我最大的“D'oh”时刻! I've been compiling it on Eclipse rather than cmd which was where I was executing it.我一直在 Eclipse 上编译它,而不是在我执行它的 cmd 上编译它。 So my newly compiled classes went to the bin folder and the compiled class file via command prompt remained the same in my src folder.所以我新编译的类转到 bin 文件夹,通过命令提示符编译的类文件在我的 src 文件夹中保持不变。 I recompiled with my new code and it works like a charm.我用我的新代码重新编译,它就像一个魅力。

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();
}           

Your code works fine for me.你的代码对我来说很好。 It replaced the text in the file as expected and didn't append.它按预期替换了文件中的文本并且没有追加。

If you wanted to append, you set the second parameter in如果要追加,则在中设置第二个参数

new FileWriter(fnew,false);

to true;为真;

SOLVED解决了

My biggest "D'oh" moment!我最大的“D'oh”时刻! I've been compiling it on Eclipse rather than cmd which was where I was executing it.我一直在 Eclipse 上编译它,而不是在我执行它的 cmd 上编译它。 So my newly compiled classes went to the bin folder and the compiled class file via command prompt remained the same in my src folder.所以我新编译的类转到 bin 文件夹,通过命令提示符编译的类文件在我的 src 文件夹中保持不变。 I recompiled with my new code and it works like a charm.我用我的新代码重新编译,它就像一个魅力。

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();
}   

Add one more line after initializing file object初始化文件对象后再添加一行

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

This simplifies it a bit and it behaves as you want it.这稍微简化了它,它的行为就像你想要的那样。

FileWriter f = new FileWriter("../playlist/"+existingPlaylist.getText()+".txt");

try {
 f.write(source);
 ...
} catch(...) {
} finally {
 //close it here
}

The easiest way to overwrite a text file is to use a public static field.覆盖文本文件的最简单方法是使用公共静态字段。

this will overwrite the file every time because your only using false the first time through.`这将每次都覆盖文件,因为您第一次只使用 false。`

public static boolean appendFile;

Use it to allow only one time through the write sequence for the append field of the write code to be false.使用它只允许一次通过写入序列的写入代码的追加字段为假。

// 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();
 }           

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM