How to list only N files in directory using java

我们两清 提交于 2019-11-29 14:40:25

Which is why you should use java.nio.file. Using Java 8:

final Path baseDir = Paths.get("path/to/dir");

final List<Path> tenFirstEntries;

final BiPredicate<Path, BasicFileAttributes> predicate = (path, attrs)
    -> attrs.isRegularFile() && path.getFileName().endsWith(".processed");

try (
    final Stream<Path> stream = Files.find(baseDir, 1, predicate);
) {
    tenFirstEntries = stream.limit(10L).collect(Collectors.toList());
}

Using Java 7:

final Path baseDir = Paths.get("path/to/dir");

final List<Path> tenFirstEntries = new ArrayList<>(10);

final DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>()
{
    @Override
    public boolean accept(final Path entry)
    {
        return entry.getFileName().endsWith(".processed")
            && Files.isRegularFile(entry);
    }
};

try (
    final DirectoryStream<Path> stream 
        = Files.newDirectoryStream(baseDir, filter);
) {
    final Iterator<Path> iterator = stream.iterator();
    for (int i = 0; iterator.hasNext() && i < 10; i++)
        tenFirstEntries.add(iterator.next());
}

Unlike File.listFiles(), java.nio.file use lazily populated streams of directory entries.

One more reason to ditch File. This is 2015 after all.

In Java 8, you can directly use Files.walk() to create a Stream of Path:

Path folder = Paths.get("...");
final int nbFilesToFound = 10;
List<Path> collect = Files.walk(folder)
                          .filter(p -> Files.isRegularFile(p) && !p.getFileName().toString().endsWith(".processed"))
                          .limit(nbFilesToFound)
                          .collect(Collectors.toList());

In Java 7, you should not use DirectoryStream.Filter if you want that the files iteration stops as soon as the number of files to find is reached. You could create a SimpleFileVisitor implementation to achieve it.

Whatever the number of files, to achieve such a requirement : extracting a specific number of files matching to a predicate from a directory, using SimpleFileVisitor appear straighter and more efficient than DirectoryStream.Filter.
So I think that it should be favored.
See my answer in this duplicate to see how to implement it.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!