I am making a game and have started to work on a class called SaveManager, so far I have had no problems except when it comes to deleting the game.
My problem is this, .delete() returns false the majority of times, without calling an exception.
If I make a loop (below) it does eventually delete, despite me not actually changing any variables or references and the game loop itself definitely doesn’t deal with the files at all, so it isn’t “releasing” the file itself at any point.
public void delete(File save)
{
while (save.exists() && !save.delete())
{
i++;
}
new infoBox("Times delete called: " + i, "delete method in saveManager");
So my question is: If the reference is not from one of my classes how do I find it and remove it, allowing me to remove the above loop, which basically hijacks the game until the file is gone.
Here is the code which does deal with the file, in case I am in fact doing something wrong (God Forbid!) I’m pretty sure this does “release” the save file in question…
private void save()
{
ObjectOutputStream OOS = null;
BufferedOutputStream BOS = null;
FileOutputStream FOS = null;
try
{
File saveFile = new File(saveDirectory + hub.world.saveName + ".dat");
OOS = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(saveFile, false)));
OOS.write... (List of Objects & Strings here)
OOS.flush();
OOS.close();
game.saved = true; //Used for warning message on exit
this.setSavesList();
}
catch (Exception e)
{
new errorWindow(e, "SaveFile, I/O error");
}
finally
{
try
{
FOS.flush();
FOS.close();
BOS.flush();
BOS.close();
OOS.flush();
OOS.close();
}
catch (IOException e)
{
hub.frame.errorWindow(e, "Problem closing streams");
}
}
}
One general thing to check while dealing with files is if you’re closing any opened streams before attempting to delete it.
You can’t delete a file unless the stream opened is closed.
Take a look at this.