简体   繁体   English

Java:如何从保存在路径中的多个文件夹中提取最大名称到文件夹名称都是数字的变量中?

[英]Java: How to extract the largest name from multiple folders kept at a path into a variable where folder names are all numeric?

There is a directory which has many folders.有一个目录,里面有很多文件夹。 The folder names are all numeric.文件夹名称都是数字。 How to extract the folder name which has the greatest value into an integer variable in java?如何将具有最大值的文件夹名称提取到 java 中的 integer 变量中?

For example: Lets say directory .../home/user has following folders:例如:假设目录.../home/user有以下文件夹:

.../home/user/19620918

.../home/user/19620919

.../home/user/19620920

How to get x = 19620920 , where x is lets say the integer variable, using the simplest and most efficient code?如何使用最简单和最有效的代码获得x = 19620920 ,其中x可以说是 integer 变量?

Using a Files.list() , you can use the following approach:使用Files.list() ,您可以使用以下方法:

public static OptionalInt getMaxNumericFilename(Path path) {
    try (Stream<Path> files = Files.list(path)) {
        return files
            .filter(Files::isDirectory)
            .map(Path::getFileName)
            .map(Path::toString)
            .mapToInt(Integer::parseInt)
            .max();
    } catch (IOException e) {
        return OptionalInt.empty();
    }
}

Example usage:示例用法:

Path path = Path.of(".../home/user");

OptionalInt max = getMaxNumericFilename(path);
System.out.println(max.getAsInt());

If the OptionalInt is empty, there are no present directories.如果OptionalInt为空,则不存在当前目录。 If you want to add additional resiliency, you can filter if the filename is an int before parsing with Integer::parseInt which can throw an exception.如果您想增加额外的弹性,您可以在使用Integer::parseInt解析之前过滤文件名是否为int ,这可能会引发异常。

This solution will work for filename numbers up to Integer.MAX_VALUE (2.147.483.647).此解决方案适用于最大为Integer.MAX_VALUE (2.147.483.647) 的文件名编号。 Consider using long or BigDecimal if required.如果需要,请考虑使用longBigDecimal

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从json中提取所有变量名? - How to extract all variable names from json? Java:如何获取文件夹中数字名称大于给定 integer 的所有文件的列表? - Java: How to fetch a list of all files within folders with numeric names greater than a given integer? 我想使用ANTLR4从Java源文件中提取所有方法名称和变量名称 - I want to extract all method names and variable names from a java source file using ANTLR4 如何获取资产文件夹中列出的文件夹名称? 仅文件夹名称,但不包含任何文件名 - How to get folders' name listed in Assets folder? Only folders' names but not any file names Java-如何获取文件夹中的所有文件名 - Java - how to get all the file names in a folder 从java中的给定路径获取所有子父文件夹 - get all the sub parent folders from given path in java 从一个字符串中提取多个子字符串(例如“ B #####”,其中#是所有可能的数字)-Java-Android - Extract multiple substrings from a string (like “B#####” where # is all possible digits) - Java - Android 如何使用Java删除具有特定名称的文件和文件夹以及这些文件夹中的所有文件 - How to delete files AND folders with specific names, and all of the files within those folders with Java 从不同文件夹中的所有文件中找到100个最大的数字 - find 100 largest numbers from all the files present in different folders 重命名Java中具有不同名称的多个文件夹 - Rename multiple folders with different names in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM