从java中删除文件夹
在Java中,我想删除包含文件和文件夹的文件夹中存在的所有内容。
public void startDeleting(String path) {
List<String> filesList = new ArrayList<String>();
List<String> folderList = new ArrayList<String>();
fetchCompleteList(filesList, folderList, path);
for(String filePath : filesList) {
File tempFile = new File(filePath);
tempFile.delete();
}
for(String filePath : folderList) {
File tempFile = new File(filePath);
tempFile.delete();
}
}
private void fetchCompleteList(List<String> filesList,
List<String> folderList, String path) {
File file = new File(path);
File[] listOfFile = file.listFiles();
for(File tempFile : listOfFile) {
if(tempFile.isDirectory()) {
folderList.add(tempFile.getAbsolutePath());
fetchCompleteList(filesList,
folderList, tempFile.getAbsolutePath());
} else {
filesList.add(tempFile.getAbsolutePath());
}
}
}
此代码不起作用,执行此操作的最佳方法是什么?
In Java, I want to delete all the contents that are present in a folder which includes files and folders.
public void startDeleting(String path) {
List<String> filesList = new ArrayList<String>();
List<String> folderList = new ArrayList<String>();
fetchCompleteList(filesList, folderList, path);
for(String filePath : filesList) {
File tempFile = new File(filePath);
tempFile.delete();
}
for(String filePath : folderList) {
File tempFile = new File(filePath);
tempFile.delete();
}
}
private void fetchCompleteList(List<String> filesList,
List<String> folderList, String path) {
File file = new File(path);
File[] listOfFile = file.listFiles();
for(File tempFile : listOfFile) {
if(tempFile.isDirectory()) {
folderList.add(tempFile.getAbsolutePath());
fetchCompleteList(filesList,
folderList, tempFile.getAbsolutePath());
} else {
filesList.add(tempFile.getAbsolutePath());
}
}
}
This code does not work, what is the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如果您使用 Apache Commons IO,那么它只是一句:
请参阅 FileUtils.deleteDirectory()
Guava 用于支持类似功能:
这已在几个版本之前从 Guava 中删除。
虽然上面的版本非常简单,但它也非常危险,因为它在没有告诉你的情况下做出了很多假设。因此,虽然在大多数情况下它可能是安全的,但我更喜欢“官方方式”来执行此操作(从 Java 7 开始):
If you use Apache Commons IO it's a one-liner:
See FileUtils.deleteDirectory()
Guava used to support similar functionality:
This has been removed from Guava several releases ago.
While the above version is very simple, it is also pretty dangerous, as it makes a lot of assumptions without telling you. So while it may be safe in most cases, I prefer the "official way" to do it (since Java 7):
我有这样的东西:
I have something like this :
试试这个:
Try this:
嵌套文件夹可能有问题。您的代码按照找到文件夹的顺序删除文件夹,这是自上而下的,这是行不通的。如果您先反转文件夹列表,它可能会起作用。
但我建议您为此使用像 Commons IO 这样的库。
It could be problem with nested folders. Your code deletes the folders in the order they were found, which is top-down, which does not work. It might work if you reverse the folder list first.
But I would recommend you just use a library like Commons IO for this.
我发现这段代码更容易理解并且更有效:
I found this piece of code more understadable and working:
使用 FileUtils.deleteDirectory() 方法可以帮助简化递归删除目录及其下面所有内容的过程。
检查这个问题
Using the FileUtils.deleteDirectory() method can help to simplify the process of deleting directory and everything below it recursively.
Check this question
我之前为此写了一个方法。删除指定目录,如果目录删除成功则返回true。
I wrote a method for this sometime back. It deletes the specified directory and returns true if the directory deletion was successful.
您将所有(子)文件和文件夹递归存储在列表中,但使用当前代码,您将在存储子文件夹之前存储父文件夹。因此,您尝试在文件夹为空之前将其删除。试试这个代码:
You're storing all (sub-) files and folder recursively in a list, but with your current code you store the parent folder before you store the children. And so you try to delete the folder before it is empty. Try this code:
文件的 javadoc .delete()
所以文件夹必须是空的,否则删除会失败。您的代码当前首先使用最顶层的文件夹填充文件夹列表,然后是其子文件夹。由于您以相同的方式遍历列表,它将尝试在删除其子文件夹之前删除最顶层的文件夹,因此这将失败。
将这些行更改
为此
应该会导致您的代码首先删除子文件夹。
删除操作失败时也会返回 false,因此您可以检查该值以在必要时进行一些错误处理。
The javadoc for File.delete()
So a folder has to be empty or deleting it will fail. Your code currently fills the folder list with the top most folder first, followed by its sub folders. Since you iterrate through the list in the same way it will try to delete the top most folder before deleting its subfolders, this will fail.
Changing these line
to this
should cause your code to delete the sub folders first.
The delete operation also returns false when it fails, so you can check this value to do some error handling if necessary.
你应该先删除文件夹中的文件,然后删除文件夹。这样你就会递归地调用该方法。
You should delete the file in the folder first , then the folder.This way you will recursively call the method.
它将递归删除文件夹
It will delete a folder recursively