【发布时间】:2014-06-18 17:29:11
【问题描述】:
这个问题是关于两个java程序之间输入输出的重定向。我的问题的简化示例的源代码如下。
这是 prog1:
import java.io.*;
public class prog1{
public static void main(String[] args) throws IOException{
Runtime rt = Runtime.getRuntime();
Process prog2 = rt.exec("java prog2");
System.out.println("prog2 has executed.");
}
}
在一个单独的文件中,我编写了 prog2,我在其中执行 Internet Explorer 以验证执行是否成功:
import java.io.*;
public class prog2{
public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a string: ");
System.out.println("You entered " + in.readLine() + ". Starting Explorer...");
Runtime.getRuntime().exec("C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe");
}
}
这是我在运行 prog2 时看到的:
> java prog2
Enter a string: hello ** Here the program waited for my input **
You entered hello. Starting Explorer... ** Here Internet Explorer opens a new window **
如果我运行 prog1,这就是我看到的:
> java prog1
prog2 has executed. ** Internet Explorer opens a new window **
请注意,prog2 没有提示我输入,也没有输出任何内容。我的目标是实现以下目标:
> java prog1
Enter a string: hello ** Here I wish for the program to await my input **
You entered hello. Starting Explorer... ** Here I wish for an Explorer window to open **
prog2 has executed.
我相信这个问题需要对 I/O 重定向有很好的了解,但不幸的是我在这方面缺乏经验。提前谢谢大家。
德文
【问题讨论】:
标签: java io inputstream outputstream