在Android中为游戏保存/加载文件

时间:2014-10-06 20:56:54

标签: java android fileinputstream fileoutputstream

我在Android中创建了一款游戏。我已经编写了一个输入/输出的类,偏向安装位置外部。我想提出一些基本问题。首先,我使用的文件是.txt(我知道它不是保存数据的最佳方法,但我用它进行测试)。奇怪的是,当游戏结束时,它应该自动保存用户的高分,但事实并非如此,所以当我关闭应用程序并重新启动它时,高分已经消失了。我还想了解保存设置/高分/硬币等(希望有保障)的首选文件类型是什么。最后,我使用Nexus 5 whitch调试游戏没有外部存储(它应该存储在本地)。这是我的代码,提前谢谢:)。

public class AndroidFileIO implements FileIO {
    Context context;
    AssetManager assets;
    String externalStoragePath;

    public AndroidFileIO(Context context) {
        this.context = context;
        this.assets = context.getAssets();
        this.externalStoragePath = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + File.separator;
    }

    public InputStream readAsset(String fileName) throws IOException {
        return assets.open(fileName);
    }

    public InputStream readFile(String fileName) throws IOException {
        return new FileInputStream(externalStoragePath + fileName);
    }

    public OutputStream writeFile(String fileName) throws IOException {
        return new FileOutputStream(externalStoragePath + fileName);
    }

    public SharedPreferences getPreferences() {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }
}

我的游戏类有这个方法

public FileIO getFileIO() {
    return fileIO;
}

这是我加载文件的方式

Settings.load(game.getFileIO());

最后我的设置类的保存/加载方法

public static void load(FileIO files) {
    BufferedReader in = null;
    try {
        in = new BufferedReader(new InputStreamReader(
                files.readFile("mrnom.txt")));
        soundEnabled = Boolean.parseBoolean(in.readLine());
        for (int i = 0; i < 5; i++) {
            highscores[i] = Integer.parseInt(in.readLine());
        }
    } catch (IOException e) {
        // :( It's ok we have defaults
    } catch (NumberFormatException e) {
        // :/ It's ok, defaults save our day
    } finally {
        try {
            if (in != null)
                in.close();
        } catch (IOException e) {
        }
    }
}

public static void save(FileIO files) {
    BufferedWriter out = null;
    try {
        out = new BufferedWriter(new OutputStreamWriter(
                files.writeFile("mrnom.txt")));
        out.write(Boolean.toString(soundEnabled));
        for (int i = 0; i < 5; i++) {
            out.write(Integer.toString(highscores[i]));
        }

    } catch (IOException e) {
    } finally {
        try {
            if (out != null)
                out.close();
        } catch (IOException e) {
        }
    }
}

这里名为

private void updateGameOver(List<TouchEvent> touchEvents) {
    int len = touchEvents.size();
    for(int i = 0; i < len; i++) {
        TouchEvent event = touchEvents.get(i);
        if(event.type == TouchEvent.TOUCH_UP) {
            if(event.x >= 128 && event.x <= 192 &&
               event.y >= 200 && event.y <= 264) {
                if(Settings.soundEnabled)
                    Assets.click.play(1);
                //debug begin
                FileIO fileIO = game.getFileIO();
                Settings.save(fileIO);
                //debug end
                game.setScreen(new MainMenuScreen(game));
                return;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

将字符串写入save引用时,问题出在out方法中。您不是每行保存一个值,而是稍后在load方法中读取每行的值。使用当前代码,您可以将以下内容保存在 mrnom.txt 文件中:true10203040而不是true\n10\n20\n30\n40

要解决这个问题,一种方法是改变:

out.write(Boolean.toString(soundEnabled));

out.write(Boolean.toString(soundEnabled) + "\n");

out.write(Integer.toString(highscores[i]));

out.write(Integer.toString(highscores[i]) + "\n");