Reputation: 3260
I am trying to run split
command in a Java program. When I run it in the console with argument --verbose
, it prints the generated chunk as follows:
creating file 'chunk00'
creating file 'chunk01'
creating file 'chunk02'
But when I run it in a java program, these outputs will be printed after finishing the process. What I must do to get outputs while split
process is running?
I've used the following code:
ProcessBuilder pb = new ProcessBuilder("split", "-a 2", "-d", "-b 52MB","--verbose",path+"/"+db,"chunk");
pb.redirectErrorStream(true);
File workingFolder = new File("/home/hajibaba");
pb.directory(workingFolder);
Process proc = pb.start();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
// read the output from the command
String s = null;
while ((s = stdInput.readLine()) != null)
{
System.out.println(s);
}
proc.waitFor();
However, it works for a bash script that uses echo
for print same results.
Upvotes: 1
Views: 983
Reputation: 33273
I/O is line buffered in an interactive session but buffered when writing to a pipe.
To work around the problem you can turn off buffering with the unbuffer
command.
ProcessBuilder pb = new ProcessBuilder("unbuffer", "split", "-a2", "-d", "-b52MB","--verbose",path+"/"+db,"chunk");
https://unix.stackexchange.com/questions/25372/turn-off-buffering-in-pipe
Upvotes: 1