Java Path获取最后一个文件夹ID

时间:2017-10-16 11:00:14

标签: java java-io

我有一个文件夹main,其中包含许多子文件夹

1
2
4
7
10

我想获取集合中最后一个文件夹的ID。在这种情况下文件夹" 10"。我怎么能在Java 8中做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以按上次修改日期对文件夹进行排序,如:

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();
    }

您可以根据需要修改排序方法,如果您需要名称..也可以这样做