流程构建器打印输出而无需任何Sysout,我想将输出存储在列表中< string>

发布于 2025-01-24 15:00:58 字数 1025 浏览 3 评论 0原文

我想存储带有名称的文件夹列表,其中list_location在list ===>

  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线路。 它不支持这种命令.....吗?

控制台输出===&gt;

CartMS
CustomerMS
PaymentMS
ProductMS
ok!    0

I want to store the list of folders with name ending with MS present at PROJECT_LOCATION in a List ===>

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

this much code is prinitng the names of folders ending with MS, but i want to store the values in a List

I tried =>

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

but the list size is printing to be 0. It is not reading the InputStream Lines.
Does it not support for these kind of commands.....?

Console output===>

CartMS
CustomerMS
PaymentMS
ProductMS
ok!    0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蝶…霜飞 2025-01-31 15:00:58

当有更好的选择时,为什么要启动外部程序列出文件?

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

Why are you starting an external program to list files when there's a much better alternative?

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

Or without streams:

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());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文