user569322
user569322

Reputation:

Java - BufferedWriter not writing (after flush())

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.

Upvotes: 2

Views: 3080

Answers (1)

Hiro2k
Hiro2k

Reputation: 5587

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.

Upvotes: 2

Related Questions