我正在用LibGDX編寫遊戲,我試圖保存一個XML文件,但總是有一個異常(java.io.FileNotFoundException:/ data/Slugfest/teams/Team1 .xml:打開失敗:ENOENT(無此文件或目錄))保存文件時。該代碼保存文件。Android XML文件不保存
public void save() {
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result;
if (Gdx.app.getType() == ApplicationType.Android) {
result = new StreamResult(new File("/data/Slugfest/teams/" + name + ".xml"));
} else {
result = new StreamResult(new File(name + ".xml"));
}
transformer.transform(source, result);
Gdx.app.log("Slugfest", "File saved.");
} catch (TransformerException tfe) {
Gdx.app.log("Slugfest", tfe.getLocalizedMessage());
}
}
順便說一下,我的清單文件包含WRITE/READ_EXTERNAL_STORAGE權限。
我不知道'LibGDX',但在簡單的android的情況下,你需要做一個dir,如果它不可用。像'File dir = new File(「path to file」);'然後 if(dir.exists()== false){dir.exists()== false){dir.mkdirs(); } 然後用於文件 File file = new File(dir,fileName); – 2012-07-11 02:50:52