簡體   English   中英

命令行java,文件參數和管道輸入

[英]Command line java, file argument and piped input

我正在研究一個程序,我希望它能像這樣運行

my_program -o Words

“Words”是一個文件,但我也希望它能用管道輸入而不是從文件中運行,所以

grep test Words | my_program -o

如何收集和存儲數據中的管道? 如何檢查數據是否已通過管道傳輸?

要讀取管道數據,您應該從程序中的stdin讀取。

grep test Words | my_program -o

在你的例子中| grep test Words的stdout my_program stdin。 請參閱有關流,管道和重定向的文章

可以使用ScannerBufferedInputReader等標准工具從System.in讀取數據管道。

檢查相反的條件可能更容易檢查管道輸入:如果文件已被指定為參數,如果尚未指定,則從標准輸入讀取

在Java中:

  • System.in “是”stdin;
  • System.out “是”stdout;
  • System.err “是”stderr。

通過使用管道,您重定向stdin,因此foo | bar foo | bar將任何foo輸出重定向到stdout到bar的stdin。

System.in是一個InputStream 如果你想有可能從stdin和文件中讀取,一種可能性就是這樣做:

InputStream in = System.in;

if (hasOptionToReadFromFile)
    in = Files.newInputStream(Paths.get(fileName));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM