[英]Android Studio write to .properties file
我關注了將自己的屬性文件放在使用Android Studio創建的android項目中的位置? 並且我得到了一個InputStream,它可以成功地從我的.properties文件讀取。 但是,我無法寫入該.properties文件,因為沒有類似於getBaseContext()。getAssets()。open(“ app.properties”)的方法 ,該方法返回OutputStream。 我還閱讀了附加新值的Java屬性文件,但這似乎對我沒有幫助,我猜是我的文件編寫器文件名錯誤,但我也嘗試了“ assets \\ userInfo.properties” ,但該方法也不起作用。
我的.properties文件位於src \\ main \\ assets \\ userInfo.properties中
Properties props = new Properties();
InputStream inputStream = null;
try{
inputStream = getBaseContext().getAssets().open("userInfo.properties");
props.load(inputStream);
props.put("name", "smith");
FileOutputStream output = new FileOutputStream("userInfo.properties"); //this line throws error
props.store(output, "This is overwrite file");
String name = props.getProperty("name");
Log.d(TAG, "onCreate: PROPERTIES TEST NAME CHANGE: " + name);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
當前代碼拋出此錯誤:
java.io.FileNotFoundException:userInfo.properties(只讀文件系統)
您無法寫入資產文件夾,因為它位於只讀的APK內部。
改用內部或外部存儲
您無法寫入資產文件夾。 如果要更新屬性文件,則必須將它們放在其他位置。 如果您想要資產或原始文件夾中的初始版本,只需在首次使用該應用程序時將其復制到默認文件dir,然后在其中讀取/寫入即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.