1

我正在使用 LibGDX 编写游戏,并且正在尝试保存 XML 文件,但总是出现异常(java.io.FileNotFoundException: /data/Slugfest/teams/Team1.xml: open failed: ENOENT (No such文件或目录))保存文件时。此代码保存文件。

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 权限。

4

1 回答 1

1

您需要创建要保存到的目录。您应该检查它是否存在,如果没有,请创建它。像这样的东西:

if (Environment.getExternalStorageState() == null) {
                directory = new File(Environment.getDataDirectory()
                        + "/RobotiumTestLog/");
                photoDirectory = new File(Environment.getDataDirectory()
                        + "/Robotium-Screenshots/");
                /*
                 * this checks to see if there are any previous test photo files
                 * if there are any photos, they are deleted for the sake of
                 * memory
                 */
                if (photoDirectory.exists()) {
                    File[] dirFiles = photoDirectory.listFiles();
                    if (dirFiles.length != 0) {
                        for (int ii = 0; ii <= dirFiles.length; ii++) {
                            dirFiles[ii].delete();
                        }
                    }
                }
                // if no directory exists, create new directory
                if (!directory.exists()) {
                    directory.mkdir();
                }

                // if phone DOES have sd card
            } else if (Environment.getExternalStorageState() != null) {
                // search for directory on SD card
                directory = new File(Environment.getExternalStorageDirectory()
                        + "/RobotiumTestLog/");
                photoDirectory = new File(
                        Environment.getExternalStorageDirectory()
                                + "/Robotium-Screenshots/");
                if (photoDirectory.exists()) {
                    File[] dirFiles = photoDirectory.listFiles();
                    if (dirFiles.length > 0) {
                        for (int ii = 0; ii < dirFiles.length; ii++) {
                            dirFiles[ii].delete();
                        }
                        dirFiles = null;
                    }
                }
                // if no directory exists, create new directory to store test
                // results
                if (!directory.exists()) {
                    directory.mkdir();
                }
            }

这里我检查是否有SD卡,如果没有,我保存在本地,否则,我保存到SD。我还检查文件,如果存在则删除。您可能不需要它,但这是一个全面的算法,应该可以满足您的需要。你要什么就拿。

希望能帮助到你。

于 2012-07-11T03:11:15.527 回答