我正在尝试创建一个将从终端或命令行运行的程序。你将不得不在参数中提供一个文件名。我希望它能够获得程序运行的路径,然后将文件名追加到它。这将是这样的:从终端或命令行运行时获取路径
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
if (args.length > 0) {
if (args[0] instanceof String && !args[0].equals(null)) {
if (args[0].equals("compile")) {
System.out.println("File to compile:");
String fileName = scanner.next();
String path = /*get the path here*/ + fileName;
File textfile = new File(path);
if (textfile.exists()) {
Compiler compiler = new Compiler(textfile);
compiler.compile();
} else {
System.out.println("File doesn't exist");
}
}
}
}
}
如果文件名不是以“://”或“/”(分别为Windows和Unix)开头,则Java将自动使用相对路径。 –
MrHug
2014-10-20 14:13:37