简体   繁体   English

如何从 java 中的命令行参数或标准输入重定向中读取?

[英]How to read from command line parameter or stdin redirection in java?

In this stackoverflow.com response I saw how get both at same time, but not separately.这个 stackoverflow.com 响应中,我看到了如何同时获得两者,但不是分别获得。

I want to get in java command line a parameter or a stdin redirection.我想在 java 命令行中获取参数或标准输入重定向。

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 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或另一个线程。单次读取或跳过这么多字节不会阻塞,但可能会读取或跳过更少的字节

Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not.请注意,虽然 InputStream 的某些实现将返回 stream 中的总字节数,但许多不会。 It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.".使用这个方法的返回值来分配一个缓冲区来保存这个 stream 中的所有数据是不正确的。

My question is: The method available() always will know that there is a std redirection?我的问题是:方法 available() 总是会知道有一个标准重定向? Will always return > 0 when there is std redirection?当有标准重定向时总是返回 > 0?

In my tests worked fine, but I don't know if it will work fine in all implementations.在我的测试中运行良好,但我不知道它是否在所有实现中都能正常运行。

If you're piping from another program, otherprogram | myprogram如果您从另一个程序进行管道传输, otherprogram | myprogram otherprogram | myprogram , the other program might be slow providing data, so you cannot test for that. otherprogram | myprogram ,另一个程序提供数据的速度可能很慢,因此您无法对此进行测试。

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.出于这个原因,命令,特别是在 Linux 上,通常会指定参数值-以指示应从 STDIN 读取数据。

Eg例如

# No-arg call prints options
myprogram

# Arg not '-' means to use argument value
myprogram "string"

# '-' means to read STDIN
myprogram - < file

otherprogram | myprogram -

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM