Java 6 文件删除

发布于 2024-12-15 07:16:35 字数 1397 浏览 2 评论 0原文

我知道这个问题是这个问题的严重重复。 但是,我现在已经将整个页面读了两遍,某些部分读了三遍,而且我一生都不知道如何/在哪里回答它!

那么,谈谈我的问题。

我正在工作,无法使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抚笙 2024-12-22 07:16:35

您正在尝试删除()一个仍被活动的打开的 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.");

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.");
仙女 2024-12-22 07:16:35

仅当没有打开的文件处理程序时,您才能删除该文件。由于您使用 FileWriter 打开文件处理程序,因此您需要先将其关闭,然后才能将其删除。换句话说,f.delete 必须在 fw.close 之后执行,

请尝试下面的代码。我进行了更改以防止您可能发现的所有可能的错误,例如,如果 fw 为空。

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(); // flush is not needed if this is all your code does. you data
                // is automatically flushed when you close fw
} catch (Exception exc) {
    System.err.println(exc.getMessage());
} finally {// finally block is always executed.
    // fw may be null if an exception is raised in the construction 
    if (fw != null) {
        fw.close();
    }
    // checking if f is null is unneccessary. it is never be null.
    if (f.delete()) {
        System.out.println("File was successfully deleted.");
    } else {
        System.err.println("File was not deleted.");
    }
}

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 after fw.close

Try the code below. I made the changes to prevent all possible bug you may found, e.g if fw is null.

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(); // flush is not needed if this is all your code does. you data
                // is automatically flushed when you close fw
} catch (Exception exc) {
    System.err.println(exc.getMessage());
} finally {// finally block is always executed.
    // fw may be null if an exception is raised in the construction 
    if (fw != null) {
        fw.close();
    }
    // checking if f is null is unneccessary. it is never be null.
    if (f.delete()) {
        System.out.println("File was successfully deleted.");
    } else {
        System.err.println("File was not deleted.");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文