Java Process getInputStream() 方法



描述

Java Process getInputStream() 方法獲取子程序的輸入流。該流獲取從由此 Process 物件表示的程序的標準輸出流管道傳輸的資料。

宣告

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

public abstract InputStream getInputStream()

引數

返回值

此方法返回連線到子程序的標準輸出的輸入流。

異常

檢查記事本程序的輸入流示例

以下示例演示了 Process getInputStream() 方法的用法。我們為記事本可執行檔案建立了一個 Process 物件。然後使用 getInputStream() 方法檢索記事本程序的輸入流。使用輸入流 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 input stream of the process and print it
         InputStream in = p.getInputStream();
         for (int i = 0; i < in.available(); i++) {
            System.out.println("" + in.read());
         }

         // wait for 10 seconds and then destroy the process
         Thread.sleep(10000);
         p.destroy();
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

輸出

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

Creating Process...

檢查計算器程序的輸入流示例

以下示例演示了 Process getInputStream() 方法的用法。我們為計算器可執行檔案建立了一個 Process 物件。然後使用 getInputStream() 方法檢索計算器程序的輸入流。使用輸入流 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 input stream of the process and print it
         InputStream in = p.getInputStream();
         for (int i = 0; i < in.available(); i++) {
            System.out.println("" + in.read());
         }

         // wait for 10 seconds and then destroy the process
         Thread.sleep(10000);
         p.destroy();
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

輸出

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

Creating Process...

檢查Windows資源管理器的輸入流示例

以下示例演示了 Process getInputStream() 方法的用法。我們為Windows資源管理器可執行檔案建立了一個 Process 物件。然後使用 getInputStream() 方法檢索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 input stream of the process and print it
         InputStream in = p.getInputStream();
         for (int i = 0; i < in.available(); i++) {
            System.out.println("" + in.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
廣告
© . All rights reserved.