题
有什么办法可以使文本文件中的数据持续持续吗?每次用户完成游戏时,在我的程序中,他的名字和各自的分数都写入文本文件。当下一个球员来时,上一个球员就会被覆盖。由于在写入模式下写作,我不确定是否支持附加模式来节省这种分数在黑莓中...欢迎任何建议
解决方案
您应该真正使用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;
}
不隶属于 StackOverflow