[英]Can only iterate over an array or an instance of java.lang.Iterable while using FileUtils
I am trying to delete a particular folder in Java and I am using Apache Commons class to do the job. 我正在尝试删除Java中的特定文件夹,并且正在使用Apache Commons类来完成这项工作。 If there is any better option please let me know as well.
如果有更好的选择,请也告诉我。
import org.apache.commons.io.FileUtils;
FileUtils.deleteDirectory(new File(destination));
Now what I need to do is - I need to find all the directories within this folder /opt/hello/world
which is X days old (here X can be 30 days) and delete all those directory one by one. 现在,我需要做的是-我需要找到
/opt/hello/world
文件夹中所有存在X天的目录(这里X可以是30天),然后逐个删除所有这些目录。 These directories can contain files as well so once I delete directory, files will also get deleted . 这些目录也可以包含文件,因此一旦删除目录,文件也将被删除。 I was looking into the API and I saw AgeFilter class so I started using like this:
我正在查看API,并且看到了AgeFilter类,所以我开始这样使用:
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.AgeFileFilter;
private static final int DAYS_OLD = 30;
public void deleteFiles(File file) {
long purgeTime = System.currentTimeMillis() - (DAYS_OLD * 24L * 60L * 60L * 1000L);
Iterator<File> filesToDelete = FileUtils.iterateFiles(file, new AgeFileFilter(purgeTime), TRUE);
for (File aFile : filesToDelete) {
aFile.delete();
}
}
But I am getting this error in for loop. 但是我在for循环中收到此错误。 What wrong I am doing?
我在做什么错?
Can only iterate over an array or an instance of java.lang.Iterable
Update:- 更新: -
long purgeTime = System.currentTimeMillis() - (DAYS_OLD * 24L * 60L * 60L * 1000L);
AgeFileFilter filter = new AgeFileFilter(purgeTime);
File path = new File("/opt/hello/world");
File[] oldFolders = FileFilterUtils.filter(filter, path);
for (File folder : oldFolders) {
FileUtils.deleteDirectory(folder);
}
You could use FileFilterUtils.filter(IOFileFilter, File...)
instead, which will return a array of File
s matching your filter. 您可以改用
FileFilterUtils.filter(IOFileFilter, File...)
,它将返回与您的过滤器匹配的File
数组。
I'd also recommend using just about anything else then basic time arithmetic, for example Java 8 has a new Time API, you could use a Joda-Time or even Calendar
at a stretch, they will account for some of the anomalies surrounding date/time computation 我还建议您使用除基本时间算术之外的所有其他方法,例如Java 8具有新的时间API,您可以暂时使用Joda-Time或什至
Calendar
,它们将解决围绕日期/时间计算
As an example... 举个例子...
LocalDate today = LocalDate.now();
LocalDate eailer = today.minusDays(30);
Date threshold = Date.from(eailer.atStartOfDay(ZoneId.systemDefault()).toInstant());
AgeFileFilter filter = new AgeFileFilter(threshold);
File path = new File("...");
File[] oldFolders = FileFilterUtils.filter(
FileFilterUtils.directoryFileFilter(),
FileFilterUtils.filter(
filter,
path.listFiles()));
for (File folder : oldFolders) {
System.out.println(folder);
}
But which you would use ( Iterable
or array) comes down to what you need 但是,您将使用哪种(可
Iterable
或数组) Iterable
您的需求
You're facing the same problem of this guy 你正面临着这个家伙的同样问题
To safely remove from a collection while iterating over it you should use an Iterator.
要在迭代集合时安全地从集合中删除,您应该使用迭代器。
**And call the next item before delete
**并在删除之前呼叫下一个项目
Mark answered in a complete way. 马克以完整的方式回答。 Take a look here
在这里看看
List<File> files= .... Iterator<File> i = files.iterator(); while (i.hasNext()) { File s = i.next(); // must be called before you can call i.remove() // Do something i.remove(); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.