I have a folder main
with many sub folders like this
1
2
4
7
10
I would like to get the ID of the last folder in the collection. In this case folder "10". How can I do this in Java 8?
I have a folder main
with many sub folders like this
1
2
4
7
10
I would like to get the ID of the last folder in the collection. In this case folder "10". How can I do this in Java 8?
You can sort folders by last modified date like:
public static String lastFolder() throws IOException {
Stream<Path> dirList = Files.list(Paths.get("your_directoy_ath")).sorted(new Comparator<Path>() {
@Override
public int compare(Path path1, Path path2) {
Long file1Name = path1.toFile().lastModified();
Long file2Name = path2.toFile().lastModified();
return file2Name.compareTo(file1Name);
}
});
return dirList.collect(Collectors.toList()).get(0).getFileName().toString();
}
You can modify sorting method as per your need, if you need by name..can do that too