Java 中的命令行管道输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1431551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Command Line Pipe Input in Java
提问by TJ-
Here is a simple piece of code:
这是一段简单的代码:
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);
}
}
}
I execute this as : java Read < input.txt
我将其执行为:java Read < input.txt
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.
一旦 input.txt 完全通过管道传输到程序中,x 就会不断获得无限的空值。为什么呢?在将文件输入代码完成后,有没有办法使标准输入(命令行)处于活动状态?我试过关闭流并重新打开,它不起作用。重置等。
采纳答案by skaffman
By executing "java Read < input.txt"
you've told the operating system that for this process, the piped file isstandard in. You can't then switch back to the command line from inside the application.
通过执行,"java Read < 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.
如果你想这样做,然后将 input.txt 作为文件名参数传递给应用程序,从应用程序内部自己打开/读取/关闭文件,然后从标准输入读取以从命令行获取内容。
回答by SingleShot
Well, this is typical for reading in a BufferedReader
. readLine()
returns null
when end of stream is hit. Perhaps your infinite loop is the problem ;-)
嗯,这是典型的阅读BufferedReader
. 当流结束时readLine()
返回null
。也许你的无限循环是问题;-)
// try / catch ommitted
String x = null;
while( (x = f.readLine()) != null )
{
System.out.println(x);
}
回答by dogbane
You need to terminate your while loop when the line is null, like this:
当该行为空时,您需要终止 while 循环,如下所示:
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
}
catch (IOException e) {
logger.error("IOException reading System.in", e);
throw e;
}
finally {
if (in != null) {
in.close();
}
}
回答by John Gowers
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?
在将文件输入代码完成后,有没有办法使标准输入(命令行)处于活动状态?
Sorry to bump an old question, but none of the answers so far points out that there isa (shell-only) way to pass back to console input after piping in a file.
对不起,撞一个老问题,但没有答案至今指出,目前是一个(壳唯一的)文件中的管道后回传给控制台输入。
If you run the command
如果你运行命令
{ cat input.txt & cat; } | java Read
then the text from input.txt
will be passed to java Read
and you will then be dropped back to console input.
然后文本 frominput.txt
将被传递到java Read
,然后您将被放回控制台输入。
Note that if you then press Ctrl+D, you will get the infinite loop of null
s, unless you modify your program to end the loop when it receives null.
请注意,如果您然后按Ctrl+ D,您将获得null
s的无限循环,除非您修改程序以在收到 null 时结束循环。