The file does not work correctly in Java

advertisements

So, here's the problem. I'm making a game, and I'm having trouble with the file saving class that saves your progress. It is able to load default values in the file to create a new game, then restarts the game so the changes can take effect. The thing is, the default values are saved, but the game no longer recognizes them. then, when I manually delete the file, the same exact method is used to re-create the file, then the game recognizes it. Here is the class I used:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import mario.simulator.MarioSimulator;

/**
 *
 * @author Jaca
 */
public class filer {
    private static File f;
    private static BufferedReader br;
    private static BufferedWriter bw;
    private boolean docterlicense;
    private boolean driverlicense;
    private boolean policelicense;
    private boolean finishedschool;
    private boolean bullied;
    private boolean messedtubby;
    private boolean toadmode;
    private long candy;
    private long coin;
    private long spaghetti;
    private long cake;
    private long water;
    private long mushroom;
    private long bomb;
    private long gun;
    private int hunger;
    private long respawn;
    private long swag;
    private long awesome;

public filer(){
    f = new File("resource\\config\\save.sav");
    if(!f.exists()){
        try {
            f.createNewFile();
            filer.reset();
            br = new BufferedReader(new FileReader(f));
            docterlicense = Boolean.parseBoolean(br.readLine());
            policelicense = Boolean.parseBoolean(br.readLine());
            finishedschool = Boolean.parseBoolean(br.readLine());
            driverlicense = Boolean.parseBoolean(br.readLine());
            bullied = Boolean.parseBoolean(br.readLine());
            messedtubby = Boolean.parseBoolean(br.readLine());
            toadmode = Boolean.parseBoolean(br.readLine());
            candy = Long.parseLong(br.readLine());
            coin = Long.parseLong(br.readLine());
            spaghetti = Long.parseLong(br.readLine());
            cake = Long.parseLong(br.readLine());
            water = Long.parseLong(br.readLine());
            mushroom = Long.parseLong(br.readLine());
            bomb = Long.parseLong(br.readLine());
            gun = Long.parseLong(br.readLine());
            hunger = Integer.parseInt(br.readLine());
            respawn = Long.parseLong(br.readLine());
            swag = Long.parseLong(br.readLine());
            awesome = Long.parseLong(br.readLine());
            MarioSimulator.loadSave(docterlicense, policelicense, finishedschool, driverlicense, bullied, messedtubby, toadmode,  candy, coin, spaghetti, cake, water, mushroom, bomb, gun, hunger, respawn, swag, awesome);
        } catch (IOException ex) {
            Logger.getLogger(filer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

public static void reset(){
    try {
        bw = new BufferedWriter(new FileWriter(f));
        bw.write("false");
        bw.newLine();
        bw.write("false");
        bw.newLine();
        bw.write("false");
        bw.newLine();
        bw.write("false");
        bw.newLine();
        bw.write("false");
        bw.newLine();
        bw.write("false");
        bw.newLine();
        bw.write("false");
        bw.newLine();
        bw.write("0");
        bw.newLine();
        bw.write("0");
        bw.newLine();
        bw.write("0");
        bw.newLine();
        bw.write("0");
        bw.newLine();
        bw.write("0");
        bw.newLine();
        bw.write("0");
        bw.newLine();
        bw.write("0");
        bw.newLine();
        bw.write("0");
        bw.newLine();
        bw.write("100");
        bw.newLine();
        bw.write("0");
        bw.newLine();
        bw.write("0");
        bw.newLine();
        bw.write("0");
        bw.flush();
        bw.close();
    } catch (IOException ex) {
        Logger.getLogger(filer.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}

Any help is helpful.


Basically, you're ignoring the save file if it exists...

f = new File("resource\\config\\save.sav");
if(!f.exists()){
    //...
} // And then what??

Instead, you should reset the file and THEN load it anyway...

f = new File("resource\\config\\save.sav");
if(!f.exists()){
    filer.reset();
}
try (BufferedReader br = new BufferedReader(new FileReader(f))) {
    // Load the file...
} catch (IOException ex) {
    Logger.getLogger(filer.class.getName()).log(Level.SEVERE, null, ex);
}

Make sure you are managing your readers and make the best effort to ensure they are closed appropriately, remember, if you create, you should close it.

If you're using Java 7+ you can use try-with-resources as demonstrated above, otherwise you should have a finally clause which closes and resources you might have opended (each will probably need there own try-catch)