删除Java中的非空目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/560708/
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-08-11 16:13:30  来源:igfitidea点击:

Deleting non-empty directories in Java

javafile-iorecursion

提问by Martin McNulty

Supposing I have a File fthat represents a directory, then f.delete()will only delete the directory if it is empty. I've found a coupleof examplesonline that use File.listFiles()or File.list()to get all the files in the directory and then recursively traverses the directory structure and delete all the files. However, since it's possible to create infinitely recursive directory structures (in both Windowsand Linux (with symbolic links)) presumably it's possible that programs written in this style might never terminate.

假设我有一个File f代表目录,那么f.delete()只有当目录为空时才会删除该目录。我已经发现了几个例子网上说使用File.listFiles()File.list()获得目录中的所有文件,然后递归地遍历目录结构和文件全部删除。但是,由于可以创建无限递归的目录结构(在Windows和 Linux(带有符号链接)中),因此以这种风格编写的程序可能永远不会终止。

So, is there a better way to write such a program so that it doesn't fall into these pitfalls? Do I need to keep track of everywhere I've traversed and make sure I don't go around in circles or is there a nicer way?

那么,是否有更好的方法来编写这样的程序,使其不落入这些陷阱?我是否需要跟踪我走过的所有地方并确保我不绕圈子,或者有更好的方法吗?

Update:In response to some of the answers (thanks guys!) - I'd rather the code didn't follow symbolic links and stayed within the directory it was supposed to delete. Can I rely on the Commons-IO implementation to do that, even in the Windows case?

更新:为了回应一些答案(谢谢大家!) - 我宁愿代码不遵循符号链接并留在它应该删除的目录中。即使在 Windows 的情况下,我也可以依靠 Commons-IO 实现来做到这一点吗?

采纳答案by Il-Bhima

If you really want your recursive directory deletion to follow through symbolic links, then I don't think there is any platform independent way of doing so without keeping track of all the directories you have traversed.

如果您真的希望通过符号链接执行递归目录删除,那么我认为没有任何平台独立的方法可以在不跟踪您遍历的所有目录的情况下这样做。

However, in pretty much every case I can think of you would just want to delete the actual symbolic link pointing to the directory rather than recursively following through the symbolic link.

但是,在几乎所有情况下,我都能想到您只想删除指向目录的实际符号链接,而不是递归地跟踪符号链接。

If this is the behaviour you want then you can use the FileUtils.deleteDirectorymethod in Apache Commons IO.

如果这是您想要的行为,那么您可以使用Apache Commons IO 中的FileUtils.deleteDirectory方法。

回答by Bombe

File.getCanonicalPath()will tell you the “real” name of the file, including resolved symlinks. When while scanning you come across a directory you alread know (because you stored them in a Map) bail out.

File.getCanonicalPath()将告诉您文件的“真实”名称,包括解析的符号链接。当您在扫描时遇到一个您已经知道的目录(因为您将它们存储在Map 中)退出。

回答by Morendil

If you could know which files are symlinks, you could just skip over those.

如果您知道哪些文件是符号链接,则可以跳过这些文件。

There is unfortunately no "clean" way of detecting symlinks in Java. Check out thispure Java workaround or this oneinvolving native code.

不幸的是,在 Java 中没有检测符号链接的“干净”方法。查看纯 Java 解决方法或涉及本机代码的解决方法。

回答by Joshua Fox

Try Apache Commons IOfor a tested implementation.

尝试使用Apache Commons IO进行测试实现。

However, I don't think it this handles the infinite-recursion problem.

但是,我认为这不能解决无限递归问题。

回答by Sebastian Ganslandt

At least under MacOSX, deleting a symbolic link to a directory does not delete the directory itself, and can therefore be deleted even if the target directory is not empty.

至少在 MacOSX 下,删除指向目录的符号链接不会删除目录本身,因此即使目标目录不为空也可以删除。

I assume this holds for most POSIX operating systems. And as far as I know, links under windows are also just files, and can be deleted as such from a Java program.

我认为这适用于大多数 POSIX 操作系统。而且据我所知,windows下的链接也只是文件,可以从Java程序中删除。