Java 6 File Deletion

后端 未结 2 543
孤街浪徒
孤街浪徒 2021-01-17 14:30

I am aware that this question is a raging duplicate of this question. However, I have now read that entire page twice, and some sections 3 times, and for th

2条回答
  •  被撕碎了的回忆
    2021-01-17 14:57

    You're trying to delete() a file which is still referenced by an active, open FileWriter.

    Try this:

    f = new File("myFile.txt");
    fw = new FileWriter(f);
    fw.write("This is a sentence that should appear in the file.");
    fw.flush();
    fw.close(); // actually free any underlying file handles.
    if(f.delete())
        System.out.println("File was successfully deleted.");
    else
        System.err.println("File was not deleted.");
    

提交回复
热议问题