boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - Process的问题
相关主题
怎么从键盘输入整数或float?
关于char和int的问题
Java练习题 7
如何显示load文件的正确进程
请问同时执行几个bat文件的问题
pipe & filter 问题
一道java面试题 (转载)
Re: How can I call another program from Java?
请教一个问题,thanks!
请问JSP/SERVLET和MYSQL如何实现照片上载和调用
相关话题的讨论汇总
话题: process话题: stdin1话题: 进程话题: stdin2
进入Java版参与讨论
1 (共1页)
j*******s
发帖数: 81
1
想实现“ls -l | sort”
开两个进程,一个运行ls -l,另一个运行sort,进程1的输出作为进程2的输入,可是
似乎
进程2没收到,谁能帮我看看下面代码的问题?
public void runStep0(){
try {
Process proc1 = Runtime.getRuntime().exec("ls -l");
InputStream stdin1 = proc1.getInputStream(); //
InputStreamReader isr1 = new InputStreamReader(stdin1);
BufferedReader br1 = new BufferedReader(isr1);
Process proc2 = Runtime.getRuntime().exec("sort");
InputStream stdin2 = proc1.getInputStream();
Z****e
发帖数: 2999
2
when piping two processes, the data flows like this:
-> stdin1 -> proc1 -> stdout1 -> pipe -> stdin2 -> proc2 -> stdout2
when any of the stdin/outs blocks, the whole system will block
and I think this is what happened with your code, you are reading from
stdout1 first, dump it to stdin2, then read data from stdout2, but these two
should be done concurrently
so you need a separate thread to act as the pipe, e.g.
class StreamPipe extends Thread{
private InputStream is;
private OutputStream o

【在 j*******s 的大作中提到】
: 想实现“ls -l | sort”
: 开两个进程,一个运行ls -l,另一个运行sort,进程1的输出作为进程2的输入,可是
: 似乎
: 进程2没收到,谁能帮我看看下面代码的问题?
: public void runStep0(){
: try {
: Process proc1 = Runtime.getRuntime().exec("ls -l");
: InputStream stdin1 = proc1.getInputStream(); //
: InputStreamReader isr1 = new InputStreamReader(stdin1);
: BufferedReader br1 = new BufferedReader(isr1);

1 (共1页)
进入Java版参与讨论
相关主题
请问JSP/SERVLET和MYSQL如何实现照片上载和调用
How to write a file to the same directory of the class file?
问个io的问题
guessContentTypeFromStream(InputStream)总是返回null怎么办?
InputStream.read() 被block的问题(陷入无限等待)
Java练习题 11
read from multiple inputstreams at the same time?
java applet读网络文件的问题
Windows 下 Java console application 的问题
请问哪里能找到radixsort java code?
相关话题的讨论汇总
话题: process话题: stdin1话题: 进程话题: stdin2