1

Hi am working on a game which saves several states once my save button is cliked, such as: 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 (e.g. 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.

copeg
  • 8,290
  • 19
  • 28
malzo
  • 13
  • 3

3 Answers3

2

A common way to do this is to use java.util.Properties to read key-value pairs from a text file. In this complete example, a property file is used to initialize a glossary of AlphaComposite rules. An enum of the rules is displayed in a 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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Huh, I didn't know that, when I first read from input files I was working in c++ so I didn't have access to Java's extensive library. Good to know, but also a caution for anybody reading this, unless you are planning on using only Java forever, make sure that you know how to solve a problem without the library before you use the library, as things which are fundamental in Java are often altogether absent in other languages. `toString()` is my favorite example. – Mitchell Carroll May 05 '15 at 22:51
  • 1
    @MitchellCarroll: I try to leverage the libraries, and I find that the Java designs inform my own designs in other venues. Kudos for addressing the OP's question in context. – trashgod May 06 '15 at 01:07
  • Same, when I do c++ I always make sure to add a `toString()` function to every object I create. Leveraging Java's libraries can greatly increase your productivity when working with it, but becoming dependent on them (Java was the first language I really committed myself to learning) can be a handicap in other languages, especially c/c++ – Mitchell Carroll May 06 '15 at 01:33
1

An ArrayList is not necessary for reading a single save file into the working memory. 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. 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.

Mitchell Carroll
  • 479
  • 5
  • 13
0

Loop through each line using readLine() and save them to an 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);
        }
fix
  • 1,425
  • 16
  • 27