下面是将要执行的一些java命令:
java -jar myJARfile.jar -huff-c input_file output_file
爪哇-jar myJARfile.jar -huff-d input_file output_file
爪哇-jar myJARfile.jar -lzw-c input_file output_file
爪哇-jar myJARfile.jar -lzw-d input_file output_file
我想知道如何访问每个参数-huff|lzw、-d|c和我主板上的文件,这样我就可以执行相应的代码了。他们都被认为是args吗?我会使用args、args1、args2、args3来访问它们吗?或者-huff|lzw-d|c不被认为是are,如果是的话,我如何访问它们?
发布于 2020-02-12 04:54:31
这就是使用main()方法的args参数的用途。对此进行测试的一种方法是:
public static void main(String args[]) {
// Just for Testing args[]...
for (String str : args) {
System.out.println(arg);
}
System.exit(0);
// --------------------------
// --- Your method code here ---
}对于不同的参数,您需要决定它们是否需要按照特定的顺序排列。如果没有,则需要为此编写代码,例如,这里有一种处理这种情况的方法:
// Class member variables (possible default values should be considered)...
private static String inputFile = ""; // Input File Path and File Name [enclosed within quotation marks if whitespaces are in path]
private static String outputFile = ""; // Output File Path and File Name [enclosed within quotation marks if whitespaces are in path]
private static String compressionType = ""; // Alternatives: HUFFMAN (-huff) or LZW (-lzw).
private static String direction = "COMPRESS"; // Alternatives: COMPRESS (-c) or DECOMPRESS (-d).
// The main() method...
public static void main(String args[]) {
// Process Command-Line Arguments provided in any order...
for (String argument : args) {
switch (argument.toLowerCase()) {
case "-huff":
compressionType = "HUFFMAN";
continue;
case "-lzw":
compressionType = "LZW";
continue;
case "-c":
direction = "COMPRESS";
continue;
case "-d":
direction = "DECOMPRESS";
continue;
}
// Source (input) and Destination (output) files...
if (new File(argument).exists() && new File(argument).isFile()) {
inputFile = argument;
}
else {
outputFile = argument;
}
}
// ------------------------------------------------
System.out.println();
System.out.println("Input File: " + inputFile);
System.out.println("output File: " + outputFile);
System.out.println("Compression Type: " + compressionType);
System.out.println("Direction: " + direction);
// Do what you want with the variables contents ....
}https://stackoverflow.com/questions/60178221
复制相似问题