Java 6文件删除 [英] Java 6 File Deletion

查看:110
本文介绍了Java 6文件删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题是这个问题。 然而,我现在已经阅读了整个页面两次,有些部分已经阅读了3次,而对于我的生活,我看不出它是如何/在哪里被回答的!

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!

所以,关于我的问题。

我正在工作,我被困在使用Java 6 SE而且无法升级到7.我正在写一个创建文件,写入文件,进行一些处理,然后需要删除文件的程序。我遇到了与上面提到的问题的人完全相同的问题:Java不会删除文件而我无法弄清楚原因。

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.

代码:

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)。

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

用户问我引用的问题回答了他自己的问题,但是这样做(以我的拙见)在任何情况下,我都无法理解它。他/她似乎暗指使用 BufferedReader 作为反对 FileInputStream 为他/她做了不同,但我不知道这是怎么回事。

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 se ems已经通过引入 java.nio.file.Files 类解决了这个问题,但同样,我不能将Java 7用于我的范围之外的原因控制。

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.

对该引用问题的其他回答者暗示这是Java中的错误,并提供各种规避,例如明确调用 System.gc()等我已经尝试了所有这些并且它们无法正常工作。

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.

推荐答案

你试图删除()仍然被活动引用的文件,打开FileWriter。

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

试试这个:

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.");

这篇关于Java 6文件删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆