1

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")

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
SANECE04
  • 11
  • 6
  • note that on linux [not all filesystems record the creation time](http://unix.stackexchange.com/q/7562). You may want to encode that information in some meta file or the filenames instead or consider using one of the other times, such as last modified. – the8472 Oct 14 '15 at 12:19

2 Answers2

2

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);
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
1

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.

Community
  • 1
  • 1
Yogesh_D
  • 17,656
  • 10
  • 41
  • 55