在Windows命令行中,如何将cat的输出提供给我的java main方法?

时间:2013-11-20 21:45:24

标签: java windows command-line-arguments command-prompt

我无法弄清楚如何让命令行命令“cat”和我的普通Java程序在Windows命令行中一起工作。就像在Linux中使用它一样:

cat *.* | grep stackoverflow

我想在Windows中执行此操作:

cat inputfile.txt | java Boil

我不能让猫喂Boil。正如您在下面看到的,命令行命令“cat”工作正常,Boil程序工作正常如果我提供的参数格式为“java 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

任何想法都非常感激。提前谢谢。

2 个答案:

答案 0 :(得分:1)

您应该从标准输入流中读取,而不是尝试将行读作参数。

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得到任何参数。

答案 1 :(得分:0)

如果您的命令是java Boil,则没有参数。因此,循环中的代码都不会执行。您正在尝试使用cat命令将其输出提供给命令的参数,但您无法在Windows中执行此操作。 (使用|也不会在Unix / Linux上执行此操作。您必须使用反引号来完成此操作。但Windows不支持反引号。)

(P.S。你在Linux上发出的命令:

cat *.* | grep stackoverflow

不会做你认为它做的事情。在模式之后,“stackoverflow”,grep期望查看要搜索的文件的文件名,或者如果没有,则使用标准输入。 |cat的输出传递给grep的标准输入,而不是命令行参数。您无法使用此选项将cat的输出用作grep模式 [除非有某种方法可以使用-f选项]。 )