Java - 无法从InputStream读取,直到OutputStream关闭

时间:2018-01-13 13:16:44

标签: java bufferedreader bufferedwriter

我试图读取和写入bash / cmd。我正在使用流程构建器启动流程。 读取和写入命令行是第一次完成。但是当我在命令行中写入内容时,我无法从InputStream中读取内容,直到我关闭了我的缓冲编写器。当我关闭缓冲的写入器时,我可以再次从命令行读取。 当我这样做时,outputStream和inputStream之间是否存在冲突:

Thread thread = new Thread() {
        public void run()
        {
            try
            {
                ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", "jdb" + path);
                processBuilder.redirectErrorStream(true);
                Process process = processBuilder.start();
                BufferedReader bufferedReaderInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
                BufferedReader bufferedReaderError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
                String currentLine = "";
                while ((currentLine = bufferedReaderInput.readLine()) != null)
                {
                    bufferedWriter.write("hello");
                    bufferedWriter.flush();
                    //If I close the bufferedWriter here, I can read again from the bufferedReader.
                }
                while ((currentLine = bufferedReaderError.readLine()) != null)
                {
                    consoleWindow.appendText(currentLine);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    };
    thread.start();

1 个答案:

答案 0 :(得分:0)

尝试更改:

bufferedWriter.write("hello");

为:

bufferedWriter.write("hello\n");

没有'\ n'就像在jdb中键入“hello”但不按Enter键。