gpt4 book ai didi

java - 读/写 txt 文件以保存

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:10 28 4
gpt4 key购买 nike

我现在已经在游戏结束时将高分写入文本文件,并在游戏加载时读取它们。我现在遇到的问题是 txt 文件 highscores.txt 在任何地方都找不到,因为我还没有创建它。是否可以在找不到文件时创建文件?相关代码如下:

在游戏结束时将高分写入文件:

if(gameOver == true){
sbg.enterState(5, new FadeOutTransition(), new FadeInTransition());
if(score > Highscores.highscore3 && score < Highscores.highscore2){
Highscores.highscore3 = score;
}else if(score > Highscores.highscore2 && score < Highscores.highscore1){
Highscores.highscore3 = Highscores.highscore2;
Highscores.highscore2 = score;
}else if(score > Highscores.highscore1){
Highscores.highscore3 = Highscores.highscore2;
Highscores.highscore2 = Highscores.highscore1;
Highscores.highscore1 = score;
}else Highscores.highscore1 = score;

//Write highscores to highscores.txt
try{
PrintWriter writer = new PrintWriter("highscores.txt", "UTF-8");
writer.println(String.valueOf(Highscores.highscore1));
writer.println(String.valueOf(Highscores.highscore1));
writer.println(String.valueOf(Highscores.highscore1));
writer.close();
}catch(Exception e){
e.printStackTrace();
}
gameOver = false;
gameStart = false;
}

在程序启动时从 highscores.txt 读取高分:

public static void main(String[] args) throws IOException{

BufferedReader in = new BufferedReader(new FileReader("highscores.txt"));
String line;

while((line = in.readLine()) != null){
System.out.println(line);
}
in.close();

我知道如果文件不存在我可以创建一个这样的文件:

try{
File save = new File("highscores.txt");
if (!save.exists()){
save.createNewFile();
System.out.println("\n----------------------------------");
System.out.println("The file has been created.");
System.out.println("------------------------------------");
}

但我不知道如何使用缓冲区来做到这一点。请帮忙!

最佳答案

先声明一下,我不熟悉在文件系统上存储游戏相关信息的最佳实践。话虽这么说,听起来您在开发它时正在尝试学习,因此考虑到这一点,我建议您从写入一个简单的 .txt 文件开始。

基础知识实际上已经可以在 SO 上找到:

如何创建和写入一个txt文件: How do I create a file and write to it in Java?
如何读取txt文件: Reading and displaying data from a .txt file
还有 Java 教程:https://docs.oracle.com/javase/tutorial/essential/io/file.html

我会做的是:
1.弄清楚何时要触发写入。
您想什么时候更新您的高分文件?对我来说,这看起来像是在 ln 158 左右的 Gameplay 类中,您在其中确定游戏已经结束:

//Change to GameOverEasy state if gameOver is true
if(gameOver == true){


2.确定何时需要读入文件以填充 Highscores。在 pastebin 中查看你的代码,对我来说,这似乎是你想要在启动时在你的 Blop 类的主要方法中做的事情,在你开始加载游戏状态/循环之前:

        public static void main(String[] args) throws IOException{

AppGameContainer appgc;
try{
appgc = new AppGameContainer(new Blop(NAME));

//Window size
appgc.setDisplayMode(960, 640, false);
appgc.setShowFPS(false);
appgc.setAlwaysRender(true);
appgc.setTargetFrameRate(60);
appgc.setVSync(true);
Display.setIcon(new ByteBuffer[] {
new ImageIOImageData().imageToByteBuffer(ImageIO.read(new File("res/images/general/Icon16.png")), false, false, null),
new ImageIOImageData().imageToByteBuffer(ImageIO.read(new File("res/images/general/Icon32.png")), false, false, null)
});

//TODO: Read in high scores file and populate Highscores class

appgc.start();
}catch(SlickException e){
e.printStackTrace();
}


3.弄清楚如何格式化文本文件。您已经在订购乐谱,因此您也可以那样存储它们。每行保存一个分数或使用“,”或“|”等分隔符将与您目前拥有的一起工作。


从简单的文本文件开始,然后根据需要更改为其他文件。如果这是一个测试/学习项目,我强烈建议您保持简单并坚持使用简单的 .txt 文件,直到您更深入地了解它

需要注意的几点:

  1. 您将遇到很多异常处理。读入该文件时,您需要确定您的游戏是否因无法加载高分文件而无法启动?另一方面,当您无法写入该文件时,它是一个严重错误吗?
  2. Always be closing

编辑:关于您的后续问题,您想使用您正在使用的文件的实际名称。 Javadocs are your friend .关于你的第二个问题,查看一些other SO posts .它覆盖得很好。

关于java - 读/写 txt 文件以保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33290861/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com