Java Process getErrorStream() 方法



描述

Java Process getErrorStream() 方法獲取子程序的錯誤流。該流獲取從由該 Process 物件表示的程序的錯誤輸出流中管道傳輸的資料。

宣告

以下是 java.lang.Process.getErrorStream() 方法的宣告

public abstract InputStream getErrorStream()

引數

返回值

此方法返回連線到子程序錯誤流的輸入流。

異常

檢查記事本程序錯誤流示例

以下示例顯示了 Process getErrorStream() 方法的使用。我們為記事本可執行檔案建立了一個 Process 物件。然後使用 getErrorStream() 方法檢索記事本程序的錯誤流。使用錯誤流的 available() 方法,我們讀取可用的錯誤。然後使用 Thread.sleep() 方法,程式暫停 10 秒,最後使用 destroy() 方法終止程序。

package com.tutorialspoint;

import java.io.InputStream;

public class ProcessDemo {

   public static void main(String[] args) {
      try {
         // create a new process
         System.out.println("Creating Process...");
         String[] cmds = {"notepad.exe"};
         Process p = Runtime.getRuntime().exec(cmds);

         // get the error stream of the process and print it
         InputStream error = p.getErrorStream();
         for (int i = 0; i < error.available(); i++) {
            System.out.println("" + error.read());
         }

         // wait for 10 seconds and then destroy the process
         Thread.sleep(10000);
         p.destroy();

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

輸出

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

Creating Process...

檢查計算器程序錯誤流示例

以下示例顯示了 Process getErrorStream() 方法的使用。我們為計算器可執行檔案建立了一個 Process 物件。然後使用 getErrorStream() 方法檢索計算器程序的錯誤流。使用錯誤流的 available() 方法,我們讀取可用的錯誤。然後使用 Thread.sleep() 方法,程式暫停 10 秒,最後使用 destroy() 方法終止程序。

package com.tutorialspoint;

import java.io.InputStream;

public class ProcessDemo {

   public static void main(String[] args) {
      try {
         // create a new process
         System.out.println("Creating Process...");
         String[] cmds = {"calc.exe"};
         Process p = Runtime.getRuntime().exec(cmds);

         // get the error stream of the process and print it
         InputStream error = p.getErrorStream();
         for (int i = 0; i < error.available(); i++) {
            System.out.println("" + error.read());
         }

         // wait for 10 seconds and then destroy the process
         Thread.sleep(10000);
         p.destroy();

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

輸出

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

Creating Process...

檢查 Windows 資源管理器程序錯誤流示例

以下示例顯示了 Process getErrorStream() 方法的使用。我們為 Windows 資源管理器可執行檔案建立了一個 Process 物件。然後使用 getErrorStream() 方法檢索 Windows 資源管理器程序的錯誤流。使用錯誤流的 available() 方法,我們讀取可用的錯誤。然後使用 Thread.sleep() 方法,程式暫停 10 秒,最後使用 destroy() 方法終止程序。

package com.tutorialspoint;

import java.io.InputStream;

public class ProcessDemo {

   public static void main(String[] args) {
      try {
         // create a new process
         System.out.println("Creating Process...");
         String[] cmds = {"explorer.exe"};
         Process p = Runtime.getRuntime().exec(cmds);

         // get the error stream of the process and print it
         InputStream error = p.getErrorStream();
         for (int i = 0; i < error.available(); i++) {
            System.out.println("" + error.read());
         }

         // wait for 10 seconds and then destroy the process
         Thread.sleep(10000);
         p.destroy();

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

輸出

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

Creating Process...
java_lang_process.htm
廣告