Java 6 文件删除
我知道这个问题是这个问题的严重重复。 但是,我现在已经将整个页面读了两遍,某些部分读了三遍,而且我一生都不知道如何/在哪里回答它!
那么,谈谈我的问题。
我正在工作,无法使用 Java 6 SE,无法升级到 7。我正在编写一个程序,该程序创建一个文件,写入文件,进行一些处理,然后需要删除该文件。我遇到了与上面提到的问题的人完全相同的问题:Java 不会删除该文件,我不明白为什么。
代码:
File f = null;
FileWriter fw = null;
try
{
f = new File("myFile.txt");
fw = new FileWriter(f);
fw.write("This is a sentence that should appear in the file.");
fw.flush();
if(f.delete())
System.out.println("File was successfully deleted.");
else
System.err.println("File was not deleted.");
}
catch(Exception exc)
{
System.err.println(exc.getMessage());
}
catch(Error er {
System.err.println(er.getMessage());
}
catch(Throwable t)
{
System.err.println(t.getMessage());
}
finally
{
fw.close();
}
它不会抛出任何异常、错误或异常(我添加这些是为了排除任何和所有边缘情况)。第二条打印语句(“文件未删除。”
)正在打印到控制台。我在 Windows 7 上运行此程序,并写入一个我拥有完全权限 (rwx) 的文件夹。
提出我引用的问题的用户回答了他自己的问题,但(以我的愚见)以一种不那么直接的方式这样做。无论如何,我很难理解它。他/她似乎在暗示这样一个事实:使用 BufferedReader 相对于 FileInputStream 对他/她产生了影响,但我只是不明白这是如何做到的适用。
Java 7 似乎通过引入 java.nio.file.Files 类解决了这个问题,但同样,由于超出我控制范围的原因,我无法使用 Java 7。
该引用问题的其他回答者暗示这是 Java 中的一个“错误”,并给出了各种规避措施,例如显式调用 System.gc() 等。我已经尝试了所有这些并且他们没有工作。
也许有人可以添加一个新的视角并为我带来一些思考。
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 the life of me I don't see how/where it is answered!
So, on to my problem.
I am at work and am stuck using Java 6 SE and cannot upgrade to 7. I am writing a program that creates a file, writes to it, does some processing, and then needs to delete the file out of existence. I am having the exact same problem as the person who asked the question I reference above: Java will not delete the file and I cannot figure out why.
The code:
File f = null;
FileWriter fw = null;
try
{
f = new File("myFile.txt");
fw = new FileWriter(f);
fw.write("This is a sentence that should appear in the file.");
fw.flush();
if(f.delete())
System.out.println("File was successfully deleted.");
else
System.err.println("File was not deleted.");
}
catch(Exception exc)
{
System.err.println(exc.getMessage());
}
catch(Error er {
System.err.println(er.getMessage());
}
catch(Throwable t)
{
System.err.println(t.getMessage());
}
finally
{
fw.close();
}
It is not throwing any throwables, errors or exceptions (I included those to rule out any and all edge cases). The second print statement ("File was not deleted."
) is being printed to the console. I am running this on Windows 7 and am writing to a folder where I have full permissions (rwx).
The user asking the question I referenced answered his own question, but does so (in my humble opinion) in a not-so-straight-forward way. In any case, I am having trouble making sense of it. He/she seems to be alluding to the fact that using a BufferedReader
as opposed to a FileInputStream
made the difference for him/her, but I just don't see how that applies.
Java 7 seems to have fixed this issues with the introduction of a java.nio.file.Files
class, but again, I can't use Java 7 for reasons outside the scope of my control.
Other answerers to that referenced question allude that this is a "bug" in Java, and give all sorts of circumventions, such as explicitly calling System.gc()
, etc. I have tried all of these and they are not working.
Maybe someone can add a fresh perspective and jog some thinking for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试删除()一个仍被活动的打开的 FileWriter 引用的文件。
试试这个:
You're trying to delete() a file which is still referenced by an active, open FileWriter.
Try this:
仅当没有打开的文件处理程序时,您才能删除该文件。由于您使用
FileWriter
打开文件处理程序,因此您需要先将其关闭,然后才能将其删除。换句话说,f.delete
必须在fw.close
之后执行,请尝试下面的代码。我进行了更改以防止您可能发现的所有可能的错误,例如,如果 fw 为空。
You can only delete the file if there is no file handler left opened. Since you open the file hanlder using
FileWriter
, you will need to close it before you can delete it. In other word,f.delete
must be executed afterfw.close
Try the code below. I made the changes to prevent all possible bug you may found, e.g if fw is null.