Overwriting txt file in java

自闭症网瘾萝莉.ら 提交于 2019-11-29 05:26:41

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;

Add one more line after initializing file object

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

SOLVED

My biggest "D'oh" moment! I've been compiling it on Eclipse rather than cmd which was where I was executing it. 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. I recompiled with my new code and it works like a charm.

                File fold=new File("../playlist/"+existingPlaylist.getText()+".txt");
            fold.delete();
            //File temp=new File("../playlist/temp.txt");
            File fnew=new File("../playlist/"+existingPlaylist.getText()+".txt");
            String source = textArea.getText();
            System.out.println(source);
            /*FileWriter f2;
            PrintWriter pw;

            try {
                f2 = new FileWriter(fnew,false);
                pw= new PrintWriter(f2);
                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));
                }*/


            try {
                FileWriter f2 = new FileWriter(fnew, false);
             f2.write(source);
                f2.close();
                //fnew.renameTo(fold);
                //fold.renameTo(temp);
                //temp.deleteOnExit();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   

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.`

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();
 }           
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!