如何将一些值传递给我的 java.io.FileFilter 实现?
How can I pass some values to my implementation of java.io.FileFilter?
我是这样实现的java.io.FileFilter
:
//get a list of files that:
// aren't directories
// have lastmodified times over X seconds in the past.
//And only get first Y files, even if there are more present.
private File[] getInputFiles(String inputDirectory) {
return new File(inputDirectory).listFiles(new FileFilter() {
int counter = 2; //TODO parameterize as Y
@Override
public boolean accept(File pathname) {
counter--;
return !pathname.isDirectory()
&& pathname.lastModified() <
System.currentTimeMillis() - 30 * 1000 //TODO parameterize as X
&& counter >= -1;
}
});
}
如您所见,我的代码中有幻数。我想用参数替换它们。
我尝试了 new FileFilter(int X, int Y) {...}
,但这是一个编译时错误。
注意:Java 7.(我认为一些 Java 8 的特征可以帮助这里,对吧?不幸的是,这应该是 Java 7.)
既然您使用的是 Java 7+,请不要理会 File
。使用 java.nio.file.
而你想要的是 DirectoryStream.Filter<Path>
。它具有与 FileFilter
几乎相同的签名,优点是您将用于从目录 (Files.newDirectoryStream()
) 获取条目的方法实际上 return 和 延迟加载的目录条目迭代器,其中File
将加载所有条目。
public final class MyFilter
implements DirectoryStream.Filter<Path>
{
// Now minus 30 seconds
private final FileTime timestamp
= FileTime.fromMillis(System.currentTimeMillis() - 30_000);
private int counter;
public MyFilter(final int initialCounter)
{
counter = initialCounter;
}
@Override
public boolean accept(final Path entry)
{
counter--;
if (!Files.isRegularFile(entry))
return false;
if (Files.getLastModifiedTime(entry).compareTo(timestamp) >= 0)
return false;
return counter > 0;
}
}
然后:
private List<Path> getInputFiles(final String baseDir, final int counter)
{
final Path path = Paths.get(baseDir);
final List<Path> result = new ArrayList<>();
final DirectoryStream.Filter<Path> filter
= new MyFilter(counter);
try (
final DirectoryStream<Path> stream = Files.newDirectoryStream(path, filter);
) {
for (final Path entry: stream)
result.add(entry);
}
return result;
}
我是这样实现的java.io.FileFilter
:
//get a list of files that:
// aren't directories
// have lastmodified times over X seconds in the past.
//And only get first Y files, even if there are more present.
private File[] getInputFiles(String inputDirectory) {
return new File(inputDirectory).listFiles(new FileFilter() {
int counter = 2; //TODO parameterize as Y
@Override
public boolean accept(File pathname) {
counter--;
return !pathname.isDirectory()
&& pathname.lastModified() <
System.currentTimeMillis() - 30 * 1000 //TODO parameterize as X
&& counter >= -1;
}
});
}
如您所见,我的代码中有幻数。我想用参数替换它们。
我尝试了 new FileFilter(int X, int Y) {...}
,但这是一个编译时错误。
注意:Java 7.(我认为一些 Java 8 的特征可以帮助这里,对吧?不幸的是,这应该是 Java 7.)
既然您使用的是 Java 7+,请不要理会 File
。使用 java.nio.file.
而你想要的是 DirectoryStream.Filter<Path>
。它具有与 FileFilter
几乎相同的签名,优点是您将用于从目录 (Files.newDirectoryStream()
) 获取条目的方法实际上 return 和 延迟加载的目录条目迭代器,其中File
将加载所有条目。
public final class MyFilter
implements DirectoryStream.Filter<Path>
{
// Now minus 30 seconds
private final FileTime timestamp
= FileTime.fromMillis(System.currentTimeMillis() - 30_000);
private int counter;
public MyFilter(final int initialCounter)
{
counter = initialCounter;
}
@Override
public boolean accept(final Path entry)
{
counter--;
if (!Files.isRegularFile(entry))
return false;
if (Files.getLastModifiedTime(entry).compareTo(timestamp) >= 0)
return false;
return counter > 0;
}
}
然后:
private List<Path> getInputFiles(final String baseDir, final int counter)
{
final Path path = Paths.get(baseDir);
final List<Path> result = new ArrayList<>();
final DirectoryStream.Filter<Path> filter
= new MyFilter(counter);
try (
final DirectoryStream<Path> stream = Files.newDirectoryStream(path, filter);
) {
for (final Path entry: stream)
result.add(entry);
}
return result;
}