简体   繁体   English

如何从Java的进程输出中实时读取BufferedReader?

[英]How do I read from BufferedReader real-time from a process output in Java?

I am trying to read real-time the data from a process using a BufferedReader and redirect it to a TextArea. 我正在尝试使用BufferedReader从进程中实时读取数据并将其重定向到TextArea。 However, I have noticed that when the process is running the .bat, it tends to freeze and cause a lag to the JavaFX TextArea. 但是,我注意到,当进程运行.bat时,它倾向于冻结并导致JavaFX TextArea滞后。 The ".bat" fiel that runs prints out a ..... one one line to indicate progress, and I believe this is where it is failing at. 运行的“ .bat”文件打印出一条.....一行以指示进度,我认为这是失败的地方。 I had an idea to have the program wait a certain amount of time, then it executes, but because its all on one line it also fails. 我有一个想法,让程序等待一定的时间,然后执行,但是因为它全部都在一行上,所以它也会失败。 Please help 请帮忙

Code: 码:

 while(iterator.hasNext()) {
    Map.Entry mentry = (Map.Entry)iterator.next();
    String taskPath = " /k d: && cd DATA\\Virtualization\\Users && ESXRun.bat";
    ProcessBuilder pb = new ProcessBuilder("cmd",taskPath);
    Process process = pb.start();
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));

    String s = "";
    // read the output from the command

    while ((s = stdInput.readLine()) != null) {
        //TextArea
        cliLog.appendText(s);                       
        cliLog.appendText("\n");
    }
    process.waitFor();
    process.destroy();
}

This is just the concept to demonstrate the issue. 这只是演示问题的概念。 You have to customize it and handle exceptions. 您必须对其进行自定义并处理异常。

public class TextAreaBash extends Application implements Runnable {

    private final TextArea textArea = new TextArea();

    public static void main(final String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(final Stage primaryStage) throws Exception {
        primaryStage.setScene(new Scene(new VBox(textArea), 300, 200));
        primaryStage.show();
        ping();
    }

    public void ping() {
        new Thread(this).start();
    }

    @Override
    public void run() {
        try {
            final ProcessBuilder processBuilder = new ProcessBuilder("cmd", "/C", "ping -a www.google.com -n 10");
            final Process process = processBuilder.start();
            final InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream());
            while (appendText(inputStreamReader)) {
                ;
            }
            process.waitFor();
            process.destroy();
        } catch (final Exception ex) {
            ex.printStackTrace();
        }
    }

    private boolean appendText(final InputStreamReader inputStreamReader) {
        try {
            final char[] buf = new char[256];
            final int read = inputStreamReader.read(buf);
            if (read < 1) {
                return false;
            }
            Platform.runLater(() -> {
                textArea.appendText(new String(buf));
            });
            return true;
        } catch (final IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}

ping TextArea

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM