Setting number in a file to 0 when pressing a JButton

In the starting menu for my game I wan't to make it so that when you press the "Continue" button you get the score that you had the last time you played. I have already fixed the code that saves the score and the code that loads the score so it isn't any problems with the continue button.

But for the "New Game" button I'm having some difficulties. When that button is pressed I need to make the file that stores the score reset (set the number inside it to 0 instead of the last score). I have tried to have this code inside the ActionListener for the button but it doesn't set the text inside the .dat file to 0 but it adds a 0 to the end.

    try{
        String zero = "0";
        byte[] reset = zero.getBytes();
        player.getFile().write(reset);
    }catch (IOException ex){

    }

I have also tried player.setScore("0"); which I use to add 10 score every time an enemy dies but then it will still get the score from the .dat file.

This is the two method inside the player class that handels the loading and saving of the file.

    public void save(){
        try{
            getScore = new File("Score.dat");
            scoreFile = new FileOutputStream(getScore);
            byte[] saveScore = score.getBytes();
            scoreFile.write(saveScore);
        }catch(FileNotFoundException ex){

        }catch(IOException ex){

        }
    }

    public void load(){
        try{
            br = new BufferedReader(new FileReader("Score.dat"));
            score = br.readLine();
        }catch (FileNotFoundException ex){

        }catch(IOException ex){

        }
    }

I have also tried to only run the load() method if I press the "Continue" button and not the "New Game" button but the I get a java.lang.NullPointerException error.

Answers


Your code is hiding all the relevant exceptions, which will make it very hard to figure out what's going on - put something like ex.printStackTrace(); in the catch blocks so you can see if exceptions are thrown. I would imagine in your case, the bytes aren't getting written because you aren't closing or flushing the stream.

Regardless, you probably want to use something like a PrintWriter with the file instead, rather than writing raw bytes:

PrintWriter writer = new PrintWriter(new File("file.txt"));
writer.println("0");
writer.close();

Need Your Help

Adding custom fields on asp net membership users table

asp.net

I have a application which I would like to add custom fields to the users table. I am not sure how to work after adding the columns. The columns are foreign keys to another table which holds more d...

-moz-inline-box vs -moz-inline-stack

css firefox properties

What's the difference between Firefox's -moz-inline-box and -moz-inline-stack proprietary display values?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.