Java ProcessBuilder redirectErrorStream() 方法



描述

Java ProcessBuilder redirectErrorStream() 方法指示此程序生成器是否合併標準錯誤和標準輸出。如果此屬性為 true,則隨後由此物件的 start() 方法啟動的子程序生成的任何錯誤輸出都將與標準輸出合併,以便可以使用 Process.getInputStream() 方法讀取兩者。這使得更容易將錯誤訊息與相應的輸出相關聯。初始值為 false。

宣告

以下是 java.lang.ProcessBuilder.redirectErrorStream() 方法的宣告

public boolean redirectErrorStream()

引數

返回值

此方法返回此程序生成器的 redirectErrorStream 屬性

異常

從 Process Builder 示例獲取記事本的重定向錯誤流詳細資訊

以下示例顯示了 ProcessBuilder redirectErrorStream() 方法的使用。在此程式中,我們建立了一個字串陣列,並將 notepad.exe 和 test.txt 新增到其中。使用該列表,我們初始化了一個 ProcessBuilder 例項。現在使用 redirectErrorStream() 方法,我們檢索了重定向錯誤流並列印了相應的對映標誌。

package com.tutorialspoint;

public class ProcessBuilderDemo {

   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);

      // check if errorstream is redirected
      System.out.println(pb.redirectErrorStream());
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

false

從 Process Builder 示例獲取計算器的重定向錯誤流詳細資訊

以下示例顯示了 ProcessBuilder redirectErrorStream() 方法的使用。在此程式中,我們建立了一個字串陣列,並將 calc.exe 新增到其中。使用該列表,我們初始化了一個 ProcessBuilder 例項。現在使用 redirectErrorStream() 方法,我們檢索了重定向錯誤流並列印了相應的對映標誌。

package com.tutorialspoint;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

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

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

      // check if errorstream is redirected
      System.out.println(pb.redirectErrorStream());
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

false

從 Process Builder 示例獲取 Windows 資源管理器的重定向錯誤流詳細資訊

以下示例顯示了 ProcessBuilder redirectErrorStream() 方法的使用。在此程式中,我們建立了一個字串陣列,並將 explorer.exe 新增到其中。使用該列表,我們初始化了一個 ProcessBuilder 例項。現在使用 redirectErrorStream() 方法,我們檢索了重定向錯誤流並列印了相應的對映標誌。

package com.tutorialspoint;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

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

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

      // check if errorstream is redirected
      System.out.println(pb.redirectErrorStream());
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

false
java_lang_processbuilder.htm
廣告