In the game that I'm working on I have a String
that displays the score, the problem is that the code save the String
to the file. I wan't the file to save the score constantly because the score will update as you play the game. Therefor I run the method from a swing Timer like this.
Timer timer = new Timer(1, new ActionListener(){
public void actionPerformed(ActionEvent e){
save();
}
});
timer.start();
The save method looks like this and it should work because I have used the exact same code for a text program I made awhile ago.
public void save(){
try{
getScore = new File("Score.dat");
scoreFile = new FileOutputStream(getScore);
byte[] saveScore = score.getBytes();
scoreFile.write(saveScore);
scoreFile.close();
}catch(FileNotFoundException ex){
}catch(IOException ex){
}
}
I have two buttons in the starting screen for the game New Game
and Continue
. I have made it so that if you press New Game
the file will update it's text to 0 and if you press Continue
the code will go on as normal and the score will equal to what is displayed in the file. This is the ActionListener
for the two buttons.
public void actionPerformed(ActionEvent e){
Object command = e.getSource();
if(command.equals(newGame)){
try{
PrintWriter pw = new PrintWriter(new File("Score.dat"));
pw.print("0");
pw.close();
}catch (FileNotFoundException ex){
}
startGame = true;
}
if(command.equals(contGame)){
startGame = true;
}
}
The file is created just normal and I don't get any errors but the file doesn't update it will always be a 0 inside it. I have tried to use PrintWriter
instead of FileOutputStream
because it works for setting the file's text to 0 but when I use it with pw.print(score);
my file explorer crash and I have to restart my comuter.
EDIT:
I have figured out what the problem is but I don't know how to solve it. When I print out the score the result will be as following: 0, 0, 0, 0, 10, 0, 0, 0, 0, 20, 0, 0, 0, 0, 30 (if the number inside the file is 0, if it would be 50 you would replace all the 0's with 50) and so on.
So as you see there's only a 25% chance for the file to update the score. I have no idea why this is happening because I don't see any place in my code that could cause this.
This is the code that loads the score back.
public void load(){
try{
br = new BufferedReader(new FileReader("Score.dat"));
score = br.readLine();
}catch (FileNotFoundException ex){
}catch(IOException ex){
}
}
This method is runned once by having load();
in the public Player()
method ( Player
is the name of the class).
I hate to suggest this, but are you certain that your score
variable is non-zero in the save()
method? You might consider putting a line in there to print the value of score
for debugging purposes, or check the value in your debugger. There is nothing obviously wrong with this method, and it does work without a flush()
, as a PrintWriter
will autoflush on close()
as defined in Writer
.
As backslash noted, you probably want to increase the sleep time on your timer, one ms is really short.
You could be having sync issues. make just one method that takes the score and write it to file. Call this method on new game, continue, close window (if it is a java frame) , and in your timer. But make the timer at least 250 milli seconds
synchronized void save(int score) {
PrintWriter pw = null;//any resource with IO should be in a finally block to close
try {
pw = new PrintWriter(new File("Score.dat"));
pw.print("" + score);
pw.close();
} catch (Exception ex) {
System.out.println("save err " + ex);
ex.printStackTrace();
// or better get a logger
// logger.log(Level.WARNING,"save err " + ex ,ex);
}finally{
if (pw != null) {
try {
pw.close();
} catch (Throwable e) {
logger.log(Level.FINER, "err close :pw" , e);
}
}
}
}
EDIT : better to keep your score var as a int or long, edit your code for read accordingly
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.