0

有什么办法可以使文本文件中的数据持久化?每次用户玩完游戏时,在我的程序中,他的名字和相应的分数都会写入一个文本文件。当下一个玩家到来时,前一个玩家会被覆盖。因为我在写模式下写,我不确定是否支持附加模式来在黑莓中保存这种分数......欢迎任何建议

4

1 回答 1

3

你真的应该使用 PersistentStore 来存储这种类型的信息——它比尝试写入文件更容易使用并且可能更可靠。

但是,如果您坚持写入文件,以下是打开文件进行附加的一般代码:

private OutputStream openFileForWriting(String filePath) {
    try {
        FileConnection fconn = (FileConnection) Connector.open(filePath);
        // If no exception is thrown, then the URI is valid, but the file may or may not exist.
        if (!fconn.exists()) {
            fconn.create();  // create the file if it doesn't exist
        }
        return fconn.openOutputStream(fconn.fileSize());
    } catch (IOException ioe) {
        System.out.println("Could not open " + filePath + " for writing");
    }
    return null;
}
于 2010-11-30T20:00:42.190 回答