What is referring to my file, preventing me from calling .delete() successfully?
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 开发者_如何学Cexit
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.
You are creating three streams, and only closing the top-most one (OOS).
You should close each stream manually. I think that will fix your problem.
Also, it's often considered good practice to put the close() in a finally block, so it definitely happens.
FileOutputStream fos = new FileOutputStream(fileName);
try
{
BufferedOutputStream bos = new BufferedOutputStream(fos);
OOS = new ObjectOutputStream(bos);
OOS.write... (List of Objects & Strings here)
OOS.flush();
OOS.close();
bos.flush();
bos.close();
game.saved = true; //Used for warning message on exit
this.setSavesList();
} catch (IOException ioe)
{
}
finally
{
fos.close();
}
Otherwise, you might flush the object output stream, and the data just gets stuck in the buffered output stream, and never gets written to the file output stream.
Here's a food for thought...from the code above, you have used a try-catch block...my guess is that one of the above operations is throwing an exception because of which the out.close() operation is getting skipped...why not add a "finally" block and close the stream there...it would definitely help you save the trouble.
Are you sure your file is actually being created? In the code snippet above you have while(!save.delete()) but later on you seem to refer to it as saveFile, these may be two different files! I would first check to see if the file even exists on the disk by doing an
if(!saveFile.exists()) {
do something here
}
if it doesnt exist, a delete will just return false, you cannot delete a file that does not exist.
If you are checking the result of delete you also need to check whether the files exists. Obviously if there is not file or it has already been deleted, delete() will return false. ;)
精彩评论