SANECE04
SANECE04

Reputation: 11

How to poll file from folder in FIFO order

How to poll file from folder which copied/placed first ("FIFO" order)

scenario: if i placed 10 file in folder. how do i get first came inside the folder ("FIFO")

Upvotes: 1

Views: 306

Answers (2)

Tagir Valeev
Tagir Valeev

Reputation: 100209

Seems that you want to get the files and sort them by creation time. You can do this using Files.readAttributes(path, BasicFileAttributes.class).creationTime(). See BasicFileAttributes documentation for details.

public Stream<Path> filesByCreation(Path folder) throws IOException {
    return Files.list(folder).sorted(
           Comparator.comparing((Path path) -> {
               try {
                   return Files.readAttributes(path, BasicFileAttributes.class)
                               .creationTime();
               } catch(IOException ex) {
                   throw new UncheckedIOException(ex);
               }
           }));
}

Usage:

filesByCreation(Paths.get("/path/to/my/folder")).forEach(System.out::println);

Upvotes: 2

Yogesh_D
Yogesh_D

Reputation: 18764

I would get a File::listFiles() and then write a comparator that will sort these files based on their created time. The files time can be access using this : Determine file creation date in Java

Edit: This assumes that while copying/moving the files into this folder the created/updated timestamps are not preserved.

Upvotes: 1

Related Questions