Overwriting txt file in java

后端 未结 5 621
悲哀的现实
悲哀的现实 2020-12-17 08:10

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         


        
5条回答
  •  遥遥无期
    2020-12-17 08:29

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

提交回复
热议问题