我加載一個道具,並將其保存文件加載
File propfile=new File(getClass().getResource("credentials.properties").toURI());
prop.load(new FileInputStream(propfile));
prop.setProperty("a", username);
prop.setProperty("c", password);
prop.setProperty("b", pbKey);
prop.store(new FileOutputStream(propfile), null);
當我正常運行這在NetBeans以其優良的,當其捆綁到.jar文件,它拋出
Caused by: java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.(Unknown Source)
現在,當我使用
getClass().getResourceAsStream("credentials.properties");
我可以閱讀FIL E,但我無法保存文件,除非我用的是.toURI()
在 - >Storing changes in .properties file that has been read via getClass().getResourceAsStream
所以當我使用toURI(),當我運行它(jar文件),它會大叫說的URI is not hierarchical
當我使用getResourceAsStream
,我無法保存文件
我該怎麼辦? 屬性文件在同一個包中的類是在
@ cypronmaya:爲什麼程序需要修改jar文件?我想你需要重新審視需求/設計。考慮一下這個jar根本不可寫的情況。 – Jayan 2012-04-13 07:48:28
@Jayan我試圖建立一個桌面應用程序,其中一些參數由用戶設置,如他們的用戶名/密碼等,有點,實施,我想使用屬性文件becoz更容易。 – cypronmaya 2012-04-13 07:52:04
@ cypronmaya:您可以在jar中的屬性中設置某種默認值。將修改後的內容另存到其他位置,例如$ {user.home}。 – Jayan 2012-04-13 07:54:50