Java Data Type Tutorial - Java ProcessBuilder .redirectOutput ()








Syntax

ProcessBuilder.redirectOutput() has the following syntax.

public ProcessBuilder.Redirect redirectOutput()

Example

In the following code shows how to use ProcessBuilder.redirectOutput() method.

import java.io.File;
import java.io.IOException;
//from   w w  w  .jav a 2  s  .  c  om
public class Main {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      String[] list = {"notepad.exe", "test.txt"};

      // create the process builder
      ProcessBuilder pb = new ProcessBuilder(list);
      try {

        pb.start();
        System.out.println(pb.redirectOutput());      
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

The code above generates the following result.