Can anyone explain to me why this would throw an EofException? It really doesn’t make sense to me.
public void setSaveFile(File saveFile)
{
// Assigns the save file
this.saveFile = saveFile;
try
{
// May create the save file if it does not yet exist
saveFile.createNewFile();
// Potentially closes streams
if(!isClosed && in != null)
{
in.close();
out.close();
}
// Creates new in and out streams
in = new ObjectInputStream(new FileInputStream(saveFile)); // Decides to get thrown here...
out = new ObjectOutputStream(new FileOutputStream(saveFile));
}
catch(IOException e)
{
e.printStackTrace();
}
}
Basically, I’m trying to load a save file from the directory “data”. When the file does not exist in this folder, I create a new one. When I do this, however, this exception gets thrown.
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
This exception does not get thrown when the file already exists. Any suggestions?