- Java.lang 包类
- Java.lang - 首页
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang 包额外内容
- Java.lang - 接口
- Java.lang - 错误
- Java.lang - 异常
- Java.lang 包实用资源
- Java.lang - 有用资源
- Java.lang - 讨论
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...