简体   繁体   English

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

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

I can't figure out how to get the command line command "cat" and my trivial Java program to work together in a Windows command line. 我不知道如何让命令行命令“ cat”和我的琐碎的Java程序在Windows命令行中一起工作。 In the same way that it's common in Linux to use: 与Linux中常见的使用方式相同:

cat *.* | grep stackoverflow

I would like to do this in Windows: 我想在Windows中这样做:

cat inputfile.txt | java Boil

I can't get cat to feed Boil though. 虽然我不能让猫喂沸腾。 As you can see below, the command line command "cat" works fine, and the Boil program works fine if I supply it with parameters in the format "java Boil these are parameters". 如下所示, 如果我以“ java Boil these are parameters”格式提供参数,则命令行命令“ cat”可以正常工作,Boil程序也可以正常工作。

My Boil.java code 我的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;
                    }
                }   
            }
        }
    }
}    

I know this could be better optimized :) 我知道这可以更好地优化:)

From my C:\\Windows\\System32\\cmd.exe, 'Administrator: cmd.exe' Windows command line window, running on a computer with Windows 7 Professional SP1 32 bit OS: 从我的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

Any thoughts much appreciated. 任何想法表示赞赏。 Thank you in advance. 先感谢您。

Instead of trying to read the lines as arguments, you should be reading from the standard input stream. 您应该从标准输入流中读取内容,而不是尝试将这些行作为参数读取。

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();
}   

Piping means passing the output stream (STDOUT) to another command as STDIN. 管道传输意味着将输出流(STDOUT)作为STDIN传递给另一个命令。 In this example, Boil nor grep get any arguments. 在此示例中,Boil或grep均获取任何参数。

If your command is java Boil , there are no arguments. 如果您的命令是java Boil ,则没有参数。 Therefore, none of the code in your loop will ever execute. 因此,循环中的任何代码都不会执行。 You're trying to get the cat command to feed its output into the arguments of the command, but you can't do that in Windows. 您正在尝试使cat命令将其输出输入到命令的参数中,但是在Windows中无法做到这一点。 (Using | doesn't do that on Unix/Linux, either. You have to use backticks to accomplish that. But Windows doesn't support backticks.) (在Unix / Linux上,使用|也不这样做。您必须使用反引号来完成此操作。但是Windows不支持反引号。)

(PS The command you gave on Linux: (PS您在Linux上给出的命令:

cat *.* | grep stackoverflow

doesn't do what you seem to think it does. 并没有按照您认为的做。 After the pattern, " stackoverflow ", grep expects to see file names of files to search, or if there aren't any, it uses standard input. 模式“ stackoverflow ”之后, grep希望看到要搜索的文件的文件名,或者如果没有,则使用标准输入。 | pipes the output of cat to grep 's standard input, not to the command-line arguments. cat的输出传递给grep的标准输入,而不传递给命令行参数。 You couldn't use this to use the output of cat as the pattern for grep [unless there's some way to use the -f option for this].) 您不能使用它来将cat的输出用作grep模式 [除非有某种方法可以使用-f选项]。

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

相关问题 如何从命令行在 Java Gradle 项目中运行 main 方法? - How do I run a main method in a Java Gradle project from command line? "如何在 Windows 上从命令行运行 Java 程序?" - How do I run a Java program from the command line on Windows? 当它包含一个外部Java程序包时,如何在命令行上编译我的主Java程序? - How do I compile my main Java program on the command line when it includes an external java package? 如何将方法的输出返回到 Java 中的主方法? - How do I get the output from a method back into the main method in Java? 如何将信息从Override的ActionListener方法发送到Java中的main方法? - How do I send information from my Overridden ActionListener method to the main method in Java? 如何使Java从命令行参数输出唯一整数? - How do I get Java to output unique integers from a command-line argument? 如何在Windows 7命令行上执行.jar java程序? - How do I execute .jar java program on Windows 7 command line? Java从主方法调用命令行参数到一个单独的方法 - Java invoking command line arguments from the main method to a separate method java中这个程序的Main是什么|我如何在main中调用line方法? - What will be the Main of this program in java |How do i call the line method in main? 我可以从命令行调用main()以外的java方法吗? - Can I invoke a java method other than main() from the command line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM