ANSWER: You cannot write an int directly to a file. Convert it to a string first.
Original Question:
I know that this may sound like a nooby question, but I have nowhere else to turn.
I'm trying to make Java write the Epoch (timestamp) to a .session file (don't ask). It doesn't seem to do anything. Here is what I have:
gamesession = new File(SESSION_LOCATION); // Sets the .session file
if(gamesession.exists()){
gamesession.delete();
gamesession.createNewFile();
FileWriter stream = new FileWriter(SESSION_LOCATION);
BufferedWriter out = new BufferedWriter(stream);
out.write(GetTimestamp());
out.flush();
} // These blocks write the Epoch to the .session file for later time
else{
gamesession.createNewFile();
FileWriter stream = new FileWriter(SESSION_LOCATION);
BufferedWriter out = new BufferedWriter(stream);
out.write(GetTimestamp());
out.flush();
}
Yes, the method is DECLARED to throw a IOException (none occurs @ runtime). The problem is that, the .session file I'm writing to always turns up empty. Am I flushing wrong? Please help me shine some light on this. Thanks for your time.
FYI: The Epoch is calculated correctly, as I did System.out.println(GetTimestamp())
.
GetTimestamp() is MY method. It is static.
You need to make sure to call the out.close()
method as well. I've noticed that sometimes when dealing with Files if you don't close them the contents don't show up.
Also what type does GetTimestamp()
return? According to BufferedWriter API the write method takes an int that is supposed to be character. I doubt your timestamp is a single char.
BufferedWriter
also implements the method signature write(String)
that it inherits from Writer
- Ted Hopp 2012-04-04 05:31