应用程序文件夹 ( file://android_asset ) 是只读的,因此您不能在此文件夹中写入
在此处阅读自述文件https://github.com/apache/cordova-plugin-file
你可以使用applicationStorageDirecory(this.file.applicaTionStorageDirecory)即/data/data/:app-id:,你可以在那里写。
所以你的解决方案是
第一步。
在应用程序启动时,将应用程序文件assets/mock 中的文件msg-list.json 复制到数据存储文件中(将其添加到app.component.ts 并在平台就绪时)
this.file.copyFile(this.file.applicationDirectory + '/www/assets/mock', 'msg-list.json', this.file.applicationStorageDirectory, 'msg-list.json').then(...)
第二步。
您现在可以在应用程序的任何位置读取和写入数据存储中的文件。
要编写文件,我建议使用writeFile 函数(不是writeExistingFile),因为writeExistingFile 函数只是writeFile 选项:{replace: true} (see source code her)。使用 writeFile 函数,您可以设置所需的选项。
export interface IWriteOptions {
replace?: boolean;
append?: boolean;
truncate?: number; // if present, number of bytes to truncate file to before writing
}
所以要在数据存储中编辑文件,我会使用如下:
this.file.writeFile(this.file.applicationStorageDirectory,"msg-list.json",JSON.stringify(data), {append: true}).then(...).catch(...)
最后。始终使用数据存储文件。
注意:
使用:this.file.applicationDirectory 而不是直接使用file:///android_asset 并使用this.file.applicationStorageDirectory