Jim_CS
Jim_CS

Reputation: 4172

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. I start the process with gdb and then get the process's streams. 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") -

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(new OutputStreamWriter(proc.getOutputStream()));

and use procIn.write("some_command"), it doesn't work and gdb doesnt get the input. Anyone know why this happens?

Upvotes: 0

Views: 284

Answers (1)

Francis Upton IV
Francis Upton IV

Reputation: 19443

You have to do a flush() on the BufferedWriter to make sure it goes out.

Upvotes: 3

Related Questions