Process Builder 在没有任何系统输出的情况下打印输出,我想将输出存储在 List<String> 中

Process Builder printing the output without any sysout, i want to store the output in List<String>

我想将名称以 MS 结尾的文件夹列表存储在 PROJECT_LOCATION 列表中 ===>

  ProcessBuilder processBuilder = new ProcessBuilder().directory(Paths.get(PROJECT_LOCATION)
        .toFile())
        .command("cmd.exe",
             "/c",
             "dir /b *MS;")
        .inheritIO()
        .redirectErrorStream(true);
  Process process = processBuilder.start();

这么多代码打印以 MS 结尾的文件夹的名称,但我想将这些值存储在列表中

我试过了=>

        List<String> msList=new ArrayList<>();  
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {  
            msList.add(line);
        }
        process.waitFor();
        System.out.println("ok!    "+  msList.size());

但列表大小打印为 0。它没有读取 InputStream 行。 不支持这样的命令吗......?

控制台输出===>

CartMS
CustomerMS
PaymentMS
ProductMS
ok!    0

既然有更好的选择,为什么还要启动外部程序来列出文件?

List<String> msList;
try (Stream<Path> stream = Files.walk(Paths.get(PROJECT_LOCATION), 1)) {
    msList = stream.filter(Files::isRegularFile) // also filters out PROJECT_LOCATION itself
            .map(f -> f.getFileName().toString())
            .filter(f -> f.endsWith("MS"))
            .toList(); // for older Java versions: .collect(Collectors.toList())
}

或者没有流:

List<String> msList = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(PROJECT_LOCATION), "*MB")) {
    for (Path file : stream) {
        msList.add(file.getFileName().toString());
    }
}