簡體   English   中英

在Windows命令行上,如何將cat的輸出輸入到java main方法中?

[英]On the Windows command line, how do I feed output from cat into my java main method?

我不知道如何讓命令行命令“ cat”和我的瑣碎的Java程序在Windows命令行中一起工作。 與Linux中常見的使用方式相同:

cat *.* | grep stackoverflow

我想在Windows中這樣做:

cat inputfile.txt | java Boil

雖然我不能讓貓喂沸騰。 如下所示, 如果我以“ java Boil these are parameters”格式提供參數,則命令行命令“ cat”可以正常工作,Boil程序也可以正常工作。

我的Boil.java代碼

// Boil down lines of input code to just ;{}( and )
//          and enjoy seeing the patterns
public class Boil {
    public static void main(String[] args) {
        int outputCounter = 0;
        for (int i = 0; i < args.length; i++) {
            for (int j = 0; j < args[i].length(); j++) {
                if (args[i].charAt(j) == ';' 
                        || args[i].charAt(j) == '(' 
                        || args[i].charAt(j) == ')' 
                        || args[i].charAt(j) == '{' 
                        || args[i].charAt(j) == '}') {
                    System.out.print(args[i].charAt(j));
                    outputCounter++;
                    if (outputCounter >= 80) {
                        System.out.print("\n");
                        outputCounter = 0;
                    }
                }   
            }
        }
    }
}    

我知道這可以更好地優化:)

從我的C:\\ Windows \\ System32 \\ cmd.exe,“管理員:cmd.exe” Windows命令行窗口中,在裝有Windows 7 Professional SP1 32位操作系統的計算機上運行:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Windows\System32>cd ../../workspace1/Boil/bin

C:\workspace1\Boil\bin>ls
Boil.class     inputfile.txt

C:\workspace1\Boil\bin>cat inputfile.txt
This is a sample text file() with {
        multiple lines!;
}
C:\workspace1\Boil\bin>java Boil this should print nothing

C:\workspace1\Boil\bin>java Boil this should print a semicolon; ok?
;
C:\workspace1\Boil\bin>java Boil test all good chars ;(){}
;(){}
C:\workspace1\Boil\bin>cat inputfile.txt | java Boil

C:\workspace1\Boil\bin>java Boil < inputfile.txt

C:\workspace1\Boil\bin>java Boil < cat inputfile.txt
The system cannot find the file specified.

C:\workspace1\Boil\bin>cat inputfile.txt > java Boil
cat: Boil: No such file or directory

C:\workspace1\Boil\bin>dir
 Volume in drive C is OS
 Volume Serial Number is 0666-2986

 Directory of C:\workspace1\Boil\bin

11/20/2013  04:29 PM    <DIR>          .
11/20/2013  04:29 PM    <DIR>          ..
11/20/2013  02:28 PM               864 Boil.class
11/20/2013  04:22 PM                57 inputfile.txt
11/20/2013  04:29 PM                57 java
               3 File(s)            978 bytes
               2 Dir(s)  872,764,809,216 bytes free

C:\workspace1\Boil\bin>cat java
This is a sample text file() with {
        multiple lines!;
}
C:\workspace1\Boil\bin>rm java

任何想法表示贊賞。 先感謝您。

您應該從標准輸入流中讀取內容,而不是嘗試將這些行作為參數讀取。

try {
    BufferedReader br = 
                  new BufferedReader(new InputStreamReader(System.in));

    String input;

    while((input = br.readLine()) != null){
        System.out.println(input);
    }

} catch (IOException io){
    io.printStackTrace();
}   

管道傳輸意味着將輸出流(STDOUT)作為STDIN傳遞給另一個命令。 在此示例中,Boil或grep均獲取任何參數。

如果您的命令是java Boil ,則沒有參數。 因此,循環中的任何代碼都不會執行。 您正在嘗試使cat命令將其輸出輸入到命令的參數中,但是在Windows中無法做到這一點。 (在Unix / Linux上,使用|也不這樣做。您必須使用反引號來完成此操作。但是Windows不支持反引號。)

(PS您在Linux上給出的命令:

cat *.* | grep stackoverflow

並沒有按照您認為的做。 模式“ stackoverflow ”之后, grep希望看到要搜索的文件的文件名,或者如果沒有,則使用標准輸入。 | cat的輸出傳遞給grep的標准輸入,而不傳遞給命令行參數。 您不能使用它來將cat的輸出用作grep模式 [除非有某種方法可以使用-f選項]。

暫無
暫無

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

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