Reputation: 11
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
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
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