简体   繁体   English

读取数据以从文本文件加载游戏

[英]read data to load game from text file

Hi am working on a game which saves several states once my save button is cliked, such as: name, totalScore ... 嗨我正在开发一个游戏,一旦我的保存按钮被阻止就会保存几个状态,例如:name,totalScore ...

This is how I saved it: 这就是我保存它的方式:

public static void save()
{
    fileName = new File("Saved Game");

    try
    { 
        fw = new FileWriter(fileName,true);  
    } 
    catch (IOException e)
    {     
        e.printStackTrace();
        JOptionPane.showConfirmDialog(frame, e.toString() + "\nFail to save game.",   
                "Saved", JOptionPane.DEFAULT_OPTION);
    } 

    f = new Formatter(fw); 
    f.format("\n%s\t",level);
    f.format("\t%s\t",  nameLabel.getText());
    f.format("\t%s\t\t", updatedLabel.getText());

    f.close();


    JOptionPane.showConfirmDialog(frame, "Saving was successful.", "Game Saved",   
            JOptionPane.DEFAULT_OPTION);   
}

I want to load a game instead of starting a new one every time. 我想加载一个游戏,而不是每次都开始一个新游戏。 I can read the data from a text file and print it to screen, but don't know how to load these into the labels (eg for name) to begin the game. 我可以从文本文件中读取数据并将其打印到屏幕,但不知道如何将这些数据加载到标签(例如名称)中以开始游戏。 Any ideas? 有任何想法吗?

My code for loading the text file so far is: 到目前为止,我加载文本文件的代码是:

public static void readFromFile (String  fileName) throws IOException
{
    FileReader fr = new FileReader(fileName);
    BufferedReader br = new BufferedReader(fr);

    System.out.println(br.readLine());

    br.close();
}

I call this method at a "load" button click. 我在“加载”按钮单击时调用此方法。 Any ideas would be greatly appreciated. 任何想法将不胜感激。

A common way to do this is to use java.util.Properties to read key-value pairs from a text file. 执行此操作的常用方法是使用java.util.Properties从文本文件java.util.Properties读取键值对。 In this complete example , a property file is used to initialize a glossary of AlphaComposite rules. 在此完整示例中 ,属性文件用于初始化AlphaComposite规则的词汇表。 An enum of the rules is displayed in a JComboBox . 规则的enum显示在JComboBox You can also use the properties to initialize the default values in an instance of java.util.prefs.Preferences , as shown in the game cited here . 您也可以使用属性中的一个实例来初始化默认值java.util.prefs.Preferences ,如图中提到的游戏在这里

An ArrayList is not necessary for reading a single save file into the working memory. 将单个保存文件读入工作内存不需要ArrayList Since you wrote to the method to write the file, you know implicitly what data is where, so if your write function looked something like (this is sort of pseudocode-ish, but it gets the point across) 既然你写了编写文件的方法,你就会隐含地知道哪些数据在哪里,所以如果你的写函数看起来像(这是一种伪代码,但它得到了重点)

outFile.writeLine(foo.name)
outFile.writeLine(foo.health)
outFile.writeLine(foo.level)

With foo as the name of your instance of your character class. foo作为你的角色类实例的名称。 Your read function would look like 你的阅读功能看起来像

bar.name = inFile.getLine()
bar.health = inFile.getLine()
bar.level = inFile.getLine()

of course you need to build bar as an empty instance of your class first, as well as doing all the associated fileIO setup and housecleaning, and don't forget to return bar from your read function. 当然,您需要首先将bar构建为类的空实例,以及执行所有相关的fileIO设置和清理,并且不要忘记从读取函数返回bar

Loop through each line using readLine() and save them to an ArrayList. 使用readLine()遍历每一行并将它们保存到ArrayList。 Since you're the one who created the text file, you know what should be in each line when you retrieve the values from the array. 由于您是创建文本文件的人,因此您知道从数组中检索值时每行应该是什么。

BufferedReader in = new BufferedReader(new FileReader("path/of/text"));
        String str;     

List<String> list = new ArrayList<String>();
        while((str = in.readLine()) != null){
            list.add(str);
        }

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

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