我正在嘗試創建一個File
對象以保存我的屬性文件。我需要知道如何從我的包中指定相對路徑,因爲下面的代碼不起作用。從包中爲新文件指定相對路徑
try {
File file = new File(new File(Thread.currentThread().getContextClassLoader().getResource("").toURI()), "com/configuration/settings.properties");
try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
properties.store(fileOutputStream, null);
}
} catch (IOException | URISyntaxException ioe) {
System.out.println(ioe);
}
你試過嗎? http://stackoverflow.com/questions/573679/open-resource-with-relative-path-in-java – Gary