>我有一个关于在Java中使用String[]参数的问题:
......
public static void main(String[] args) throws Exception {
new EMR().start(args);
}
public void start(String[] args) throws Exception {
File recordFile = new File(args[0]);
File instructionFile = new File(args[1]);
File outputFile = new File(args[2]);
.......
这只是文件读取的一个例子,所以如果我想运行代码并将实际文件名/路径放入main()方法,我该如何实现它,例如,我可以这样写它吗:
new EMR().start(1.txt,2.txt.3.txt)
像这样在命令行上传递参数
C:myfolder> java HelloWorld hi java world
其中 HelloWorld 是 java 类名,hi java world 是 args,例如: hi = args[0] , java = args[1] , hello = args[2]
如果你改变你的varargs,你可以写
new EMR().start("1.txt", "2.txt", "3.txt");
public void start(String... args) throws IOException {
或者无需更改开始即可编写
new EMR().start("1.txt 2.txt 3.txt".split(" "));