我访问放置在项目文件夹中的XML文件(例如'assets'),用于打开和读取文件中的数据。
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
InputStream iS = this.getAssets().open("myFile.xml");
Document doc = docBuilder.parse(iS);
但是如果我想编辑文件,我找不到保存它的路径。
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
//create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
String xmlString = sw.toString();
OutputStream f0;
byte buf[] = xmlString.getBytes();
**f0 = new FileOutputStream("/assets/wines.xml");**
//f0 = new FileOutputStream("file:///assets/wines.xml");
for(int i=0;i<buf .length;i++) {
f0.write(buf[i]);
}
f0.close();
buf = null;
我总是获得FileNotFoundException
行:
f0 = new FileOutputStream("/assets/wines.xml");
有什么想法吗?谢谢!
答案 0 :(得分:1)
如果您尝试将edit or write
文件发送至assets or raw folder
。我想这是不可能的。 It cannot be done
答案 1 :(得分:0)
Bettery尝试将其保存到SD卡或内部存储
使用File sdDir = [Environment.getExternalStorageDirectory()][1];
获取SD卡路径。
不要忘记给予许可
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />