Redirecting the process output and error streams to file
In this recipe, we will see how to deal with the output and error streams of a process spawned from the Java code. We will write the output or error produced by the spawned process to a file.Â
Â
Â
Getting ready
In this recipe, we will make use of the iostat command. This command is used for reporting the CPU and I/O statistics for different devices and partitions. Let's run the command and see what it reports:
$ iostatNote
In some Linux distributions, such as Ubuntu, iostat is not installed by default. You can install the utility by running sudo apt-get install sysstat.
The output of the preceding command is as follows:

How to do it...
Follow these steps:
- Create a new
ProcessBuilderobject by specifying the command to be executed:
ProcessBuilder pb = new ProcessBuilder("iostat");- Redirect the output and error streams to the file's output and error, respectively:
pb.redirectError(new File("error"))
.redirectOutput(new File...