【问题标题】:On the Windows command line, how do I feed output from cat into my java main method?在 Windows 命令行上,如何将 cat 的输出输入到我的 java main 方法中?
【发布时间】:2013-12-05 03:19:00
【问题描述】:

我不知道如何让命令行命令“cat”和我的琐碎 Java 程序在 Windows 命令行中协同工作。就像在 Linux 中常用的一样:

cat *.* | grep stackoverflow

我想在 Windows 中执行此操作:

cat inputfile.txt | java Boil

但我不能让猫喂煮沸。正如您在下面看到的,命令行命令“cat”工作正常,Boil 程序工作正常如果我为它提供格式为“java Boil these are parameters”的参数。

我的 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 中,'Administrator: 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

非常感谢任何想法。提前谢谢你。

【问题讨论】:

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


    【解决方案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 获取任何参数。

    【讨论】:

    • 非常感谢,这行得通。命令行现在显示:C:\workspace1\Boil\src>cat inputfile.txt | java Boil ------命令行输出------ (){;}
    【解决方案2】:

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

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

    cat *.* | grep stackoverflow
    

    并不像你认为的那样做。在模式之后,“stackoverflow”、grep 期望看到要搜索的文件的文件名,或者如果没有,它使用标准输入。 |cat 的输出传送到grep 的标准输入,而不是命令行参数。您不能使用它来使用cat 的输出作为grep模式 [除非有某种方法可以为此使用-f 选项]。)

    【讨论】:

      猜你喜欢
      • 2016-01-03
      • 1970-01-01
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 2021-11-22
      • 2023-02-03
      • 2017-01-28
      相关资源
      最近更新 更多