我想请点击此链接传递到Java中的文件: Parsing arguments to a Java command line program解析作为参数从命令行
和使用参数在java中解析某些文件。
我需要提供给应用程序的文件主要是2.让我们称它们为file1和file2。
我希望我可以调用java这样的:
/usr/bin/java -jar myapplication.jar -f file1
或
/usr/bin/java -jar myapplication.jar -s file2
永远不要在同一时间。
在我发布的链接中,我无法理解如何将一个参数(这是一个文件)传递给void main。
我的理解之前的代码,我需要-f -s格式化参数:
public class myApp {
public static void main(String[] args) throws IOException, JAXBException, SQLException {
Database db = new Database();
if (args.length == 0) {
System.out.println("No arguments were given.\n");
db.disconnect();
}
else {
System.out.println("Got the argument.\n");
File file = new File(args[0]);
String namefile = file.getName();
switch (namefile.substring(0, 6)) {
case "FILE01":
System.out.println("Processing file type FILE01: "+namefile);
Process_filezeroone program_zeroone = new Process_filezeroone();
program_zeroone.process(file, namefile);
break;
case "FILE02":
System.out.println("Processing file type FILE02: "+namefile);
Process_filezerotwo program_zerotwo = new Process_filezerotwo();
program_zerotwo.process(file, namefile);
break;
default:
System.out.println("Skipping: "+namefile);
db.disconnect();
}
}
}
}
感谢您的任何帮助。
虽然我不完全了解你的问题,但也许你应该看看Apache Commons CLI:https://commons.apache.org/proper/commons-cli/ 另外,你不应该使用用方法和变量名称强调。 –
传递或解析?他们是完全不同的东西。 – byxor
解析一个通过args传递的文件,我认为这是正确的(?) - 好的,我正在看看那个链接,谢谢。谢谢你的提示 – Nihvel