試試這個!
if (new File("myspecialfile.gz").exists()) {
System.out.println(new File("myspecialfile.gz").getAbsolutePath());
}
我會在應用程序啓動時通過命令行參數傳遞主方法的路徑。
if (args.length > 0) {
System.out.println(new File(args[0].trim()).getAbsolutePath());
} else {
throw new IllegalArgumentException("Configuration file needed to"
+ " run. Provide configuration file's path as argument.");
}
但是,也許這種解決方案不是你所追求的。
編輯:
那麼,如果你在你的項目屬性定義新的 「運行」 配置並保存,然後將其保存到..NetBeansProjects/YOUR_PROJECT_NAME/nbproject文件/ CONFIGS/your_configuration_name.properties
在你可以像這樣定義你的命令行參數: application.args =「/ somepath/NetBeansProjects/SomeProjectName/myspecialfile.gz」
我不相信你可以給它添加$ {somedir}引用。
不,這不是我正在尋找的解決方案。我正在尋找一個可以添加到Run配置中的參數,然後最終將其作爲命令行傳遞,但後者不是問題。 – TwoThe
@edit:相信沒有幫助。 ;) – TwoThe