简体   繁体   English

从BufferedReader读取

[英]Reading from BufferedReader

I am trying to execute commands on the terminal. 我正在尝试在终端上执行命令。 using 使用

        ProcessBuilder builder = new ProcessBuilder("/bin/bash");
        builder.redirectErrorStream(true);
        Process process = builder.start();
        OutputStream stdin = process.getOutputStream();
        InputStream stdout = process.getInputStream();

        reader = new BufferedReader(new InputStreamReader(stdout));
        writer = new BufferedWriter(new OutputStreamWriter(stdin));

I am going to use this reader and and writer to continuously communicate with the process. 我将使用这个读写器来持续地与过程进行通信。

I'm using the following loop to read 我正在使用以下循环阅读

        while ((line = reader.readLine()) != null) {
        System.out.println("line);
        }

ISSUE: The problem here is that, when the reader starts reading from the buffer its forever in the while loop. 问题:这里的问题是,当读取器开始在while循环中永远从缓冲区读取数据时。 It never exits. 它永远不会退出。

I tried to put the reading in a thread 我试着把读数放在一个线程中

public void run() {
        try {
            String line;
            outputText = "";
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException ex) {
            Logger.getLogger(ThreadReader.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

But now i have no control on when the reader starts and finishes reading. 但是现在我无法控制阅读器何时开始和完成阅读。

GOAL: I need to execute a command and read the output and then the next command. 目标:我需要执行一条命令,先读取输出,然后再读取下一条命令。

Your Java program is communicating with an external interactive process. 您的Java程序正在与外部交互过程进行通信。 It has only the process's output and error streams to work with to determine how to proceed from any given point. 它仅具有流程的输出和错误流,可以用来确定如何从任何给定的点继续进行。 If you want it to recognize subdivisions of the output, such as responses to individual commands, then you need to teach it what the boundaries of those subdivisions look like. 如果您希望它识别输出的细分,例如对单个命令的响应,那么您需要教会它这些细分的边界是什么样的。

There are any number of ways you might approach the problem. 您可以采用多种方法来解决此问题。 For example, if the external program emits a prompt when it is ready to accept a new command, then it seems natural to watch for that prompt. 例如,如果外部程序准备好接受新命令时发出提示,那么监视该提示似乎是很自然的。 Alternatively, perhaps you can tweak the input to the program so as to cause it to produce a recognizable marker at the end of each command's output. 另外,也许您可​​以调整程序的输入,以使其在每个命令输出的末尾产生可识别的标记。

Do also consider that this is a solved problem (many times over). 还要考虑这是一个已解决的问题(很多次)。 The canonical utility for general-purpose scripting of interactive programs is a Tcl program called "Expect". 用于交互式程序通用脚本的规范实用程序是一个名为“ Expect”的Tcl程序。 It has inspired work-alikes in many languages, including many in Java. 它启发了许多语言的工作方式,包括Java中的许多语言。 Google gives me three distinct examples among the first five hits, but I have no specific experience with any of them so I make no recommendation. Google为我提供了前五个热门歌曲中的三个不同示例,但是我对它们中的任何一个都没有具体的经验,因此我不建议这样做。

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

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