在 Java 中退出时删除目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11165253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:03:10  来源:igfitidea点击:

Deleting a directory on exit in Java

javadirectory

提问by Arrin

I was just wondering if it was possible to delete a directory when the application was closed?

我只是想知道是否可以在应用程序关闭时删除目录?

Is there an easy way to do something when the application is closed?

当应用程序关闭时,有没有一种简单的方法来做某事?

Currently my app downloads the class files at runtime so what I'm really looking for is something to remove them from the clients system.

目前我的应用程序在运行时下载类文件,所以我真正想要的是将它们从客户端系统中删除。

I've tried so far

到目前为止我已经尝试过

File directory = new File("macros/bin/");
directory.deleteOnExit();

As well as a delete on runtime of the downloaded startup files (which obviously can't be done seeing as it needs them in order to run).

以及在运行时删除下载的启动文件(这显然无法完成,因为它需要它们才能运行)。

回答by nickgroenke

You could try this:

你可以试试这个:

Runtime.getRuntime().addShutdownHook(new Thread() {

      @Override
      public void run() {
        /* Delete your file here. */
      }
 });

A lot depends on how your program is ending.

很大程度上取决于您的程序如何结束。

回答by Jacob Eckel

You should do the following using Apache Commons IO:

您应该使用Apache Commons IO执行以下操作:

Runtime.getRuntime().addShutdownHook(new Thread(() -> FileUtils.deleteQuietly(fileOrDir)));

Note: the FileUtils.forceDeleteOnExit implementation is flawed: it registers files for deletion during registration of the directory, thus if files are added later it will fail to delete the directory.

注意:FileUtils.forceDeleteOnExit 实现存在缺陷:它在目录注册期间注册要删除的文件,因此如果稍后添加文件,将无法删除目录。

回答by Stephen C

Just for the record, the javadoc says that directory.deleteOnExit()should work ... the method supposedly works for directories. However, approach is probably failing because the directory is not empty at the point it tries to delete it ... and the OS typically won't let you delete a non-empty directory.

只是为了记录,javadoc 说directory.deleteOnExit()应该可以工作……该方法应该适用于目录。但是,方法可能会失败,因为在尝试删除目录时目录不是空的……而且操作系统通常不会让您删除非空目录。

The solution would be to call deleteOnExit()for the files that the user downloads too. You have to do this afterthe directory.deleteOnExit()call. See the javadoc for why.

解决方案是调用deleteOnExit()用户下载的文件。您必须在通话执行此操作directory.deleteOnExit()。请参阅 javadoc 了解原因。

Having said that, the explicit shutdown hook is a more robust approach.

话虽如此,显式关闭挂钩是一种更强大的方法。

回答by Frederic Leitenberger

You can use this: org.apache.commons.io.FileUtils.forceDeleteOnExit(File)

你可以使用这个: org.apache.commons.io.FileUtils.forceDeleteOnExit(File)

/*
 * Schedules a file to be deleted when JVM exits.
 * If file is directory delete it and all sub-directories.
 */
FileUtils.forceDeleteOnExit(File)

Limitation:
Unfortunately this only works if NO additional files and/or folders are created inside the directory after forceDeleteOnExitwas called.

限制:
不幸的是,这仅适用forceDeleteOnExit于调用后在目录中没有创建其他文件和/或文件夹的情况。

回答by Sreyas Adury

This worked for me. A recursive function.

这对我有用。一个递归函数。

public static void makeVolatile(File folder)
{
    folder.deleteOnExit();
    for(File f : folder.listFiles())
    {
        if(f.isDirectory())
            makeVolatile(f);
        else
            f.deleteOnExit();
    }
}