Java 中的命令行管道输入 [英] Command Line Pipe Input in Java
问题描述
这是一段简单的代码:
import java.io.*;
public class Read {
public static void main(String[] args) {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
String x = null;
try{
x = f.readLine();
}
catch (IOException e) {e.printStackTrace();}
System.out.println(x);
}
}
}
我将其执行为:java Read <输入.txt
I execute this as : java Read < input.txt
一旦 input.txt 完全通过管道传输到程序中,x 就会不断获得无限的空值.为什么呢?在将文件输入代码完成后,有没有办法使标准输入(命令行)处于活动状态?我试过关闭流并重新打开,它不起作用.重置等等.
Once the input.txt is completely piped into the program, x keeps getting infinite nulls. Why is that so? Is there a way by which I can make the Standard In(Command Line) active after the file being fed into the code is done? I've tried closing the stream and reopening, it doesn't work. Reset etc also.
推荐答案
通过执行 "java Read
By executing "java Read < input.txt"
you've told the operating system that for this process, the piped file is standard in. You can't then switch back to the command line from inside the application.
如果你想这样做,然后将 input.txt 作为文件名参数传递给应用程序,从应用程序内部自己打开/读取/关闭文件,然后从标准输入读取以从命令行获取内容.
If you want to do that, then pass input.txt as a file name parameter to the application, open/read/close the file yourself from inside the application, then read from standard input to get stuff from the command line.
这篇关于Java 中的命令行管道输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!