Java Android Studio写入.properties文件
我随后得到了一个InputStream,它成功地读取了我的.properties文件。但是,我无法写入该.properties文件,因为没有类似于getBaseContext().getAssets().open(“app.properties”)的方法返回OutputStream。我也读过,但这似乎对我没有帮助,我猜我给文件编写器的文件名是错误的,但我也尝试了“assets\userInfo.properties”,这也不起作用 My.properties文件位于src\main\assets\userInfo.properties中Java Android Studio写入.properties文件,java,android,Java,Android,我随后得到了一个InputStream,它成功地读取了我的.properties文件。但是,我无法写入该.properties文件,因为没有类似于getBaseContext().getAssets().open(“app.properties”)的方法返回OutputStream。我也读过,但这似乎对我没有帮助,我猜我给文件编写器的文件名是错误的,但我也尝试了“assets\userInfo.properties”,这也不起作用 My.properties文件位于src\main\assets
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中
改用内部或外部存储您无法写入资产文件夹。如果要更新属性文件,必须将它们放在其他地方。如果您想在assets或raw文件夹中保存初始版本,只需在首次使用应用程序时将其复制到default files dir,然后在那里读取/写入