[英]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.