Java Process getOutputStream() 方法



描述

Java Process getOutputStream() 方法獲取子程序的輸出流。輸出到流的內容被管道傳輸到此 Process 物件所代表的程序的標準輸入流。

宣告

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

public abstract OutputStream getOutputStream()

引數

返回值

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

異常

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

以下示例演示了 Process getOutputStream() 方法的用法。我們為記事本可執行檔案建立了一個 Process 物件。然後使用 getOutputStream() 方法檢索記事本程序的輸出流。使用輸出流的 close() 方法,我們關閉輸出流。然後使用 Thread.sleep() 方法,程式暫停 10 秒,最後使用 destroy() 方法終止程序。

package com.tutorialspoint;

import java.io.BufferedOutputStream;
import java.io.OutputStream;

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 output stream
         OutputStream out = p.getOutputStream();

         // close the output stream
         System.out.println("Closing the output stream...");
         out.close();
		 
         // wait for 10 seconds and then destroy the process
         Thread.sleep(10000);
         p.destroy();
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

輸出

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

Creating Process...
Closing the output stream...

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

以下示例演示了 Process getOutputStream() 方法的用法。我們為計算器可執行檔案建立了一個 Process 物件。然後使用 getOutputStream() 方法檢索計算器程序的輸出流。使用輸出流的 close() 方法,我們關閉輸出流。然後使用 Thread.sleep() 方法,程式暫停 10 秒,最後使用 destroy() 方法終止程序。

package com.tutorialspoint;

import java.io.BufferedOutputStream;
import java.io.OutputStream;

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 output stream
         OutputStream out = p.getOutputStream();

         // close the output stream
         System.out.println("Closing the output stream...");
         out.close();
		 
         // wait for 10 seconds and then destroy the process
         Thread.sleep(10000);
         p.destroy();
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

輸出

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

Creating Process...
Closing the output stream...

檢查 Windows 資源管理器程序的輸出流示例

以下示例演示了 Process getOutputStream() 方法的用法。我們為 Windows 資源管理器可執行檔案建立了一個 Process 物件。然後使用 getOutputStream() 方法檢索 Windows 資源管理器程序的輸出流。使用輸出流的 close() 方法,我們關閉輸出流。然後使用 Thread.sleep() 方法,程式暫停 10 秒,最後使用 destroy() 方法終止程序。

package com.tutorialspoint;

import java.io.BufferedOutputStream;
import java.io.OutputStream;

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 output stream
         OutputStream out = p.getOutputStream();

         // close the output stream
         System.out.println("Closing the output stream...");
         out.close();
		 
         // wait for 10 seconds and then destroy the process
         Thread.sleep(10000);
         p.destroy();
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

輸出

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

Creating Process...
Closing the output stream...
java_lang_process.htm
廣告