簡體   English   中英

在java中覆蓋txt文件

[英]Overwriting txt file in java

我編寫的代碼應該覆蓋所選文本文件的內容,但它附加了它。 我到底做錯了什么?

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

你的代碼對我來說很好。 它按預期替換了文件中的文本並且沒有追加。

如果要追加,則在中設置第二個參數

new FileWriter(fnew,false);

為真;

解決了

我最大的“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();
}   

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

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

這稍微簡化了它,它的行為就像你想要的那樣。

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

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

覆蓋文本文件的最簡單方法是使用公共靜態字段。

這將每次都覆蓋文件,因為您第一次只使用 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();
 }           

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM