英文:
How to read from command line parameter or stdin redirection in java?
问题
以下是翻译好的内容:
在这个 stackoverflow.com 的回答中,我看到了如何同时获取两者,但不是分开获取。
我想在 Java 命令行中通过参数或 stdin 重定向获取输入。
我用以下方式解决了这个问题:
public static void main(String[] args) {
String fileInput = "";
try {
if (System.in.available() > 0) {
InputStreamReader isr = new InputStreamReader(System.in);
int ch;
while((ch = isr.read()) != -1) {
//System.out.print((char)ch);
fileInput += (char)ch;
}
}
} catch(IOException e) {
e.printStackTrace();
}
if ( ((fileInput.length() > 0) && (args.length > 0)) ||
((fileInput.length() == 0) && (args.length != 1)) ||
((fileInput.length() == 0) && (args.length == 0))
) {
System.out.println("Use: myprogram \"string\"");
System.out.println("Use: myprogram < file");
System.exit(0);
}
}
但我有一个疑问。正如Oracle 10 帮助文档所说:“返回从此输入流读取(或跳过)的字节数的估计,在下一次调用此输入流的方法时,该调用不会被阻塞。下一次调用可以是相同的线程或另一个线程。读取或跳过这么多字节的单个操作不会阻塞,但可能会读取或跳过较少的字节。
注意,虽然某些 InputStream 的实现会返回流中的总字节数,但许多不会。绝不能使用此方法的返回值来分配一个用于保存此流中所有数据的缓冲区。”
我的问题是:方法 available() 是否总是会知道是否存在标准输入重定向?是否总会在存在标准输入重定向时返回 > 0?
在我的测试中运行得很好,但我不知道它是否在所有实现中都能正常工作。
英文:
In this stackoverflow.com response I saw how get both at same time, but not separately.
I want to get in java command line a parameter or a stdin redirection.
I solved it in this way:
public static void main(String[] args) {
String fileInput = "";
try {
if (System.in.available() > 0) {
InputStreamReader isr = new InputStreamReader(System.in);
int ch;
while((ch = isr.read()) != -1) {
//System.out.print((char)ch);
fileInput += (char)ch;
}
}
} catch(IOException e) {
e.printStackTrace();
}
if ( ((fileInput.length() > 0) && (args.length > 0)) ||
((fileInput.length() == 0) && (args.length != 1)) ||
((fileInput.length() == 0) && (args.length == 0))
) {
System.out.println("Use: myprogram \"string\"");
System.out.println("Use: myprogram < file");
System.exit(0);
}
But I have a doubt. As Oracle 10 help says: "Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes
Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.".
My question is: The method available() always will know that there is a std redirection? Will always return > 0 when there is std redirection?
In my tests worked fine, but I don't know if it will work fine in all implementations.
答案1
得分: 0
如果您从另一个程序进行管道传输,otherprogram | myprogram
,另一个程序可能提供数据的速度较慢,因此您无法对其进行测试。
因此,出于这个非常原因,命令通常会在Linux上指定一个参数值为-
,以表示应从标准输入(STDIN)读取数据。
例如:
# 不带参数调用打印选项
myprogram
# 参数非'-'表示使用参数值
myprogram "string"
# '-' 表示从标准输入读取
myprogram - < file
otherprogram | myprogram -
英文:
If you're piping from another program, otherprogram | myprogram
, the other program might be slow providing data, so you cannot test for that.
For this very reason, it is common for commands, especially on Linux, to specify an argument value of -
to indicate that data should be read from STDIN.
E.g.
# No-arg call prints options
myprogram
# Arg not '-' means to use argument value
myprogram "string"
# '-' means to read STDIN
myprogram - < file
otherprogram | myprogram -
专注分享java语言的经验与见解,让所有开发者获益!
评论