Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list only N files in directory using java

Tags:

java

java-io

If I have a directory contains a lot of files (about 1000 file). Some of these files named .processed and other not.

How can I list only 10 unprocessed files.

I am using this code to filter the processed file.

File[] inputFileList = inputDirectory.listFiles(new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    return !pathname.getName().endsWith(".processed");
                }
            });

But if the number of un-processed files is huge, this may lead to memory error. so I need to read a limited number of files each time the application will run.

like image 881
Fanooos Avatar asked Dec 02 '22 17:12

Fanooos


1 Answers

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.

like image 51
fge Avatar answered Dec 19 '22 11:12

fge