作者: contagious (傳染) 看板: java
標題: [合作翻譯]Java新手 12
時間: Mon Jun 17 22:46:54 2002
12.如何取得由Java程式呼叫的外在程式的執行結果?
每個application都會有一個Runtime類別的instance,讓你可以和執行時期的環境做互動。
你可以呼叫 getRuntime()來取得這個instance。
Runtime.exec()的參數指的是要執行的程式。Runtime.exec()會產生一個原生的process並
傳回一個 Process的子類別,可以用來控制或者取得這個原生process的資訊。這個process
既不使終端機介面,也不用命令列介面,而是將他的I/O操作都轉向到這三個串流:
Process.getOutputStream(), Process.getInputStream(), Process.getErrorStream().
下面是一個例子:
12345678910111213141516171819202122232425
import java.io.*;
public class ReadingProcess {
public static void main(String args[]){
//Provide a command as an input argument
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(args[0]);
BufferedWriter buffOut = new BufferedWriter(
new OutputStreamWriter(
process.getOutputStream());
BufferedReader buffIn = new BufferedReader(
new InputStreamReader(
process.getInputStream());
buffOut.write("//Some String");
buff.flush(); //Ensure that the output reaches the process
String s;
if((s=buffIn.readLine())!= null)
System.out.println(s);
buffOut.close();
buffIn.close();
}
}
注意!
根據不同的底層作業平台,你可能會遇到一些停滯的狀況。有一些平台的I/O串流只有很小
的buffer,如果不能快速的對標準I/O串流做讀寫,則可能會發生停滯的狀況。
12. How do you retrieve output from external programs that have been executed by
a program developed on the Java Platform?
Every application written on has one instance of the class Runtime to allow
interaction with its runtime environment. You retrieve this instance by calling
getRuntime().
The arguments of Runtime.exec() are commands to be executed in the runtime
environment. The Runtime.exec() methods create native processes and return a
subclass of Process which provides methods to control and retrieve information
from the underlying process. The process has neither a terminal nor a console
thus all its standard I/O operations are redirected to the parent process via
three streams: Process.getOutputStream(), Process.getInputStream(),
Process.getErrorStream().
Note the following sample:
12345678910111213141516171819202122232425
import java.io.*;
public class ReadingProcess {
public static void main(String args[]){
//Provide a command as an input argument
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(args[0]);
BufferedWriter buffOut = new BufferedWriter(
new OutputStreamWriter(
process.getOutputStream());
BufferedReader buffIn = new BufferedReader(
new InputStreamReader(
process.getInputStream());
buffOut.write("//Some String");
buff.flush(); //Ensure that the output reaches the process
String s;
if((s=buffIn.readLine())!= null)
System.out.println(s);
buffOut.close();
buffIn.close();
}
}
A note of warning - you may experience some hanging depending on the type of
native platform you are running on. Some native platforms have small buffers for
standard I/O streams, failure to quickly write/read from the standard
output/input streams into those of the subprocess may cause the subprocess to
block.
標題: [合作翻譯]Java新手 12
時間: Mon Jun 17 22:46:54 2002
12.如何取得由Java程式呼叫的外在程式的執行結果?
每個application都會有一個Runtime類別的instance,讓你可以和執行時期的環境做互動。
你可以呼叫 getRuntime()來取得這個instance。
Runtime.exec()的參數指的是要執行的程式。Runtime.exec()會產生一個原生的process並
傳回一個 Process的子類別,可以用來控制或者取得這個原生process的資訊。這個process
既不使終端機介面,也不用命令列介面,而是將他的I/O操作都轉向到這三個串流:
Process.getOutputStream(), Process.getInputStream(), Process.getErrorStream().
下面是一個例子:
12345678910111213141516171819202122232425
import java.io.*;
public class ReadingProcess {
public static void main(String args[]){
//Provide a command as an input argument
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(args[0]);
BufferedWriter buffOut = new BufferedWriter(
new OutputStreamWriter(
process.getOutputStream());
BufferedReader buffIn = new BufferedReader(
new InputStreamReader(
process.getInputStream());
buffOut.write("//Some String");
buff.flush(); //Ensure that the output reaches the process
String s;
if((s=buffIn.readLine())!= null)
System.out.println(s);
buffOut.close();
buffIn.close();
}
}
注意!
根據不同的底層作業平台,你可能會遇到一些停滯的狀況。有一些平台的I/O串流只有很小
的buffer,如果不能快速的對標準I/O串流做讀寫,則可能會發生停滯的狀況。
12. How do you retrieve output from external programs that have been executed by
a program developed on the Java Platform?
Every application written on has one instance of the class Runtime to allow
interaction with its runtime environment. You retrieve this instance by calling
getRuntime().
The arguments of Runtime.exec() are commands to be executed in the runtime
environment. The Runtime.exec() methods create native processes and return a
subclass of Process which provides methods to control and retrieve information
from the underlying process. The process has neither a terminal nor a console
thus all its standard I/O operations are redirected to the parent process via
three streams: Process.getOutputStream(), Process.getInputStream(),
Process.getErrorStream().
Note the following sample:
12345678910111213141516171819202122232425
import java.io.*;
public class ReadingProcess {
public static void main(String args[]){
//Provide a command as an input argument
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(args[0]);
BufferedWriter buffOut = new BufferedWriter(
new OutputStreamWriter(
process.getOutputStream());
BufferedReader buffIn = new BufferedReader(
new InputStreamReader(
process.getInputStream());
buffOut.write("//Some String");
buff.flush(); //Ensure that the output reaches the process
String s;
if((s=buffIn.readLine())!= null)
System.out.println(s);
buffOut.close();
buffIn.close();
}
}
A note of warning - you may experience some hanging depending on the type of
native platform you are running on. Some native platforms have small buffers for
standard I/O streams, failure to quickly write/read from the standard
output/input streams into those of the subprocess may cause the subprocess to
block.
![](https://i-blog.csdnimg.cn/blog_migrate/5a23052a871149843d8fe34672e1bd7a.gif)