Java - BufferedWriter not writing (after flush())

Go To StackoverFlow.com

2

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.

2012-04-04 05:22
by NoName
Just to be sure; do you have the right permissions to write files in the directory you are writing to - Joris 2012-04-04 05:23
Yup. At least I should - NoName 2012-04-04 05:24
is this code generating an exception at runtime? if so, could we see it - mfrankli 2012-04-04 05:24
No exception is being thrown. Any exception that appears @ runtime is later put into a try/catch block and has the stacktrace printed - NoName 2012-04-04 05:25
In your post you say "Yes, the method throws a IOException.". Now you say "No exception is being thrown." Which is it - Ted Hopp 2012-04-04 05:26
@TedHopp I think he means that the method is declared as throwing an IOException, rather than that one is actually occurring at runtim - mfrankli 2012-04-04 05:28
Can you show us the exception's stack trace - Kaushik Shankar 2012-04-04 05:30
Yes, @mfrankli, that is correct. I DECLARE the Exception - NoName 2012-04-04 05:30


2

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.

2012-04-04 05:30
by Hiro2k
GetTimestamp returns an int - NoName 2012-04-04 05:31
BufferedWriter also implements the method signature write(String) that it inherits from Writer - Ted Hopp 2012-04-04 05:31
I'll try to write a string instead. I will let you know - NoName 2012-04-04 05:33
Yeah I know, that's why I asked to be sure. A timestamp is typically a numeric value, which is also another reason that it would appear the file is empty. Because depending on the encoding of the file and the text editor the int might be an unpritable character - Hiro2k 2012-04-04 05:33
Converting GetTimestamp to a string worked. Thanks - NoName 2012-04-04 05:35
Ads