【发布时间】: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