我正在尝试在Java中做一些基本管道。我写了一个非常简单的测试程序,但代码在while循环中不断被阻塞,因为似乎没有来自cat的stdout的输入。我做错了哪一部分?从管道到另一个进程的输入流仍然被阻塞
public static void main(String[] arguments) throws Exception{
Process p=Runtime.getRuntime().exec("cat");
PrintStream out=new PrintStream(p.getOutputStream());
Scanner in=new Scanner(p.getInputStream());
out.println("hello");
while(in.hasNextLine()){
System.out.println(in.nextLine());
}
}
如果你停止猫会发生什么。你印有什么东西? – Dien