简体   繁体   English

如何使用BufferedWriter读取某一行

[英]How to read a certain line with a BufferedWriter

I am working on a simple save system for my game, which involves three methods, init load and save. 我正在为我的游戏开发一个简单的保存系统,它涉及三种方法,init加载和保存。

This is my first time trying out reading and writing to/from a file, so I am not sure if I am doing this correctly, therefore I request assistance. 这是我第一次尝试读取和写入文件,因此我不确定我是否正确地执行此操作,因此我请求帮助。

I want to do this: 我想做这个:

When the game starts, init is called. 游戏开始时,会调用init。 If the file saves does not exist, it is created, if it does, load is called. 如果文件保存不存在,则创建它,如果存在,则调用load。

Later on in the game, save will be called, and variables will be written to the file, line by line (I am using two in this example.) 稍后在游戏中,将调用save,并且变量将逐行写入文件(在此示例中我使用了两个。)

However, I am stuck on the load function. 但是,我坚持加载功能。 I have no idea what do past the point I am on. 我不知道过去的重点是什么。 Which is why I am asking, if it is possible to select a certain line from a file, and change the variable to that specific line. 这就是为什么我要问,是否可以从文件中选择某一行,并将变量更改为该特定行。

Here is my code, like I said, I have no idea if I am doing this correctly, so help is appreciated. 这是我的代码,就像我说的,我不知道我是否正确地这样做,所以请帮助。

private File saves = new File("saves.txt");

private void init(){
    PrintWriter pw = null;

    if(!saves.exists()){
        try {
            pw = new PrintWriter(new File("saves.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }else{
        try {
            load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public void save(){
    PrintWriter pw = null;

    try {
        pw = new PrintWriter(new FileOutputStream(new File("saves.txt"), true));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    pw.println(player.coinBank);
    pw.println(player.ammo);

    pw.close();
}

public void load() throws IOException{
    BufferedReader br = new BufferedReader(new FileReader(saves));
    String line;
    while ((line = br.readLine()) != null) {

    }
}

I was thinking of maybe having an array, parsing the string from the text file into a integer, putting it into the array, and then have the variables equal the values from the array. 我想可能有一个数组,将文本文件中的字符串解析为整数,将其放入数组,然后使变量等于数组中的值。

Seems like your file is a key=value structure, I suggest you'll use Properties object in java. 好像你的文件是key = value结构,我建议你在java中使用Properties对象。 Here's a good example . 这是一个很好的例子

Your file will look like this: 您的文件将如下所示:

player.coinBank=123
player.ammo=456

To save: 要保存:

Properties prop = new Properties();
prop.setProperty("player.coinBank", player.getCoinBank());
prop.setProperty("player.ammo", player.getAmmo());
//save properties to project root folder
prop.store(new FileOutputStream("player.properties"), null);

Then you'll load it like this: 然后你会像这样加载它:

Properties prop = new Properties();
prop.load(new FileInputStream("player.properties"));

//get the property value and print it out
System.out.println(prop.getProperty("player.coinBank"));
System.out.println(prop.getProperty("player.ammo"));

Reading and writing are pretty much symmetric. 阅读和写作几乎是对称的。

You're writing player.coinBank as the first line of the file, and player.ammo as the second line. 你正在写player.coinBank作为文件的第一行, player.ammo作为第二行。 So, when reading, you should read the first line and assign it to player.coinBank , then read the second line and assign it to player.ammo : 因此,在阅读时,您应该阅读第一行并将其分配给player.coinBank ,然后读取第二行并将其分配给player.ammo

public void load() throws IOException{
    try (BufferedReader br = new BufferedReader(new FileReader(saves))) {
        player.coinBank = br.readLine();
        player.ammo = br.readLine();
    }
}

Note the use of the try-with-resources statement here, which makes sure the reader is closed, whatever happens in the method. 请注意这里使用try-with-resources语句 ,这样可以确保读者关闭,无论方法发生什么。 You should also use this construct when writing to the file. 在写入文件时,您还应该使用此构造。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM