简体   繁体   English

为什么PrintWriter可以用于输出到Process,而BufferedWriter不起作用?

[英]Why does PrintWriter work for outputting to Process, but BufferedWriter does not?

I have code similar to the following that interacts with gdb from Java. 我有类似于以下与Java中的gdb交互的代码。 I start the process with gdb and then get the process's streams. 我使用gdb启动该进程,然后获取该进程的流。 I have a thread that constantly reads the output of the process (using procOut ) and prints it to the screen, and I send commands to gdb using procIn.println("some_command") - 我有一个线程不断读取进程的输出(使用procOut )并将其输出到屏幕,然后使用procIn.println("some_command")将命令发送到procIn.println("some_command")

Process proc = new ProcessBuilder("gdb").start;

procOut = new BufferedReader(new InputStreamReader(proc.getInputStream()));
procErr = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
procIn = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())));

However if I change procIn to a BufferedWriter - 但是,如果我将procIn更改为BufferedWriter-

procIn = BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));

and use procIn.write("some_command") , it doesn't work and gdb doesnt get the input. 并使用procIn.write("some_command") ,它不起作用,并且gdb无法获取输入。 Anyone know why this happens? 有人知道为什么会这样吗?

您必须在BufferedWriter上执行flush()以确保其消失。

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

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