在黑莓中写入文本文件

发布于 2024-10-05 23:54:28 字数 132 浏览 0 评论 0原文

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

is there any way out that we can make the data in text file persistent? everytime a user finishes playing a game, in my program his name and respective score is written to a text file. When the next player comes the previous one gets overwritten. since am writing in write mode, I am not sure whether append mode is supported to save scores of this sort in blackberry...any suggestions are welcome

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那支青花 2024-10-12 23:54:28

您确实应该使用 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;
}

You should really use the PersistentStore to store this type of information - it's much easier to use and probably more reliable than trying to write files.

However, if you insist on writing files, here's the general code to open a file for appending:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文