无法通过Java删除目录

发布于 2024-09-28 03:47:29 字数 272 浏览 5 评论 0原文

在我的应用程序中,我编写了从驱动器中删除目录的代码,但是当我检查文件的删除功能时,它不会删除该文件。我写过这样的内容

//Code to delete the directory if it exists
File directory = new File("c:\\Report\\");
if(directory.exists())
    directory.delete(); 

The directory is not in use: 仍然无法删除该目录。

In my application I have written the code to delete the directory from drive but when I inspect the delete function of File it doesn't delete the file. I have written some thing like this

//Code to delete the directory if it exists
File directory = new File("c:\\Report\\");
if(directory.exists())
    directory.delete(); 

The directory is not in use: still it is not able to delete the directory.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

扬花落满肩 2024-10-05 03:47:29

在Java中,目录删除只能针对空目录,这会导致类似以下的方法:

/**
 * Force deletion of directory
 * @param path
 * @return
 */
static public boolean deleteDirectory(File path) {
    if (path.exists()) {
        File[] files = path.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                deleteDirectory(files[i]);
            } else {
                files[i].delete();
            }
        }
    }
    return (path.delete());
}

这个方法将删除您的文件夹,即使非空目录,也不会出现任何麻烦(除非该目录被操作系统锁定)。

in Java, directory deletion is possible only for empty directory, which leads to methods like the following :

/**
 * Force deletion of directory
 * @param path
 * @return
 */
static public boolean deleteDirectory(File path) {
    if (path.exists()) {
        File[] files = path.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                deleteDirectory(files[i]);
            } else {
                files[i].delete();
            }
        }
    }
    return (path.delete());
}

This one will delete your folder, even if non-empty, without troubles (excepted when this directory is locked by OS).

葬﹪忆之殇 2024-10-05 03:47:29

为什么要发明一个带有递归删除方法的轮子?看看 apache commons io。
https://commons.apache.org/proper/commons-io/ javadocs/api-1.4/

FileUtils.deleteDirectory(dir);

或者

FileUtils.forceDelete(dir);

这就是您所需要的。
还有很多有用的方法来操作文件......

Why to invent a wheel with methods to delete recursively? Take a look at apache commons io.
https://commons.apache.org/proper/commons-io/javadocs/api-1.4/

FileUtils.deleteDirectory(dir);

OR

FileUtils.forceDelete(dir);

That is all you need.
There is also plenty of useful methods to manipulate files...

初见你 2024-10-05 03:47:29

查看文档:

如果此路径名表示一个目录,则该目录必须为空才能删除。

您是否确保该目录为空(也没有隐藏文件)?

Looking at the docs:

If this pathname denotes a directory, then the directory must be empty in order to be deleted.

Did you make sure that the directory is empty (no hidden files either) ?

一个人的夜不怕黑 2024-10-05 03:47:29

该目录必须为空才能删除它。如果不为空,则需要使用 File.listFiles() 和 File.delete() 递归删除它

The directory must be empty to delete it. If it's not empty, you need to delete it recursively with File.listFiles() and File.delete()

一江春梦 2024-10-05 03:47:29

另外两种可能性(除了目录不为空之外):

  • 运行 java 程序的用户没有对该目录的写入/删除权限
  • 该目录被不同的进程使用/锁定(您写的是它不是,但是您如何确认了这一点?)

Two other possibilities (besides the directory not being empty):

  • The user which runs the java program does not have write/delete permission for the directory
  • The directory is used/locked by a different process (you write that it's not, but how have you confirmed this?)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文