1

I cannot seem to write to a file. I create the file and directory successfully, and I don't hit any exceptions, yet when I open the file, it doesn't have any lines written to it.

Is it possible that I need to somehow save changes to the file? Or is there some other way that I may not end up seeing the change, even though it has supposedly been made?

File stalkerFolder = new File("plugins/logs");
File logFile = new File("plugins/logs/log.txt");
FileWriter fw;
FileReader fr;
BufferedWriter writer;
BufferedReader reader;
boolean error = false;
try{
    if(!folder.exists()){
        folder.mkdir();
    }
    logFile.createNewFile();
}catch(Exception e){}
try{
    fw = new FileWriter("plugins/logs/log.txt");
    fr = new FileReader("plugins/logs/log.txt");
    writer = new BufferedWriter(fw);
    reader = new BufferedReader(fr);
} catch(Exception e){
    System.err.println("ERROR: CANNOT READ OR WRITE TO LOG FILE");
    System.err.println("Plugin terminated.");
    error = true;
}

System.out.println("writing to log");
//Record to log
        try{
            writer.write("test log message");
        }catch(Exception e){
            System.err.println("could not write to log");
            e.printStackTrace();
        }

I do not get any errors printed out, and I DO reach "writing to log" successfully.

1
  • 1
    Like digitaljoel said, BufferedWriters only write to an internal buffer. The buffer is written to the underlying FileWriter when you call flush(). Commented Mar 23, 2012 at 20:36

5 Answers 5

6

You need to close the writer. Change the last few lines to this.

//Record to log
try{
    writer.write("test log message");
    writer.close();
}catch(Exception e){
    System.err.println("could not write to log");
    e.printStackTrace();
}

All I did was add the writer.close().

Sign up to request clarification or add additional context in comments.

4 Comments

Shouldnt there be a writer.flush() before you close as well.
Do you need to open it again before each write?
If you want to output to the file without closing it you can use writer.flush() then if you want to write more data you can do so, just make sure to flush each time(if you need to see changes in between) and then close the writer at the end of the application.
@Porthos3 No, you only need to close when you're done, after the last write.
1

If that's all of your source you are never flushing and closing the buffered writer.

2 Comments

@Inerdial Why should it be a comment? It concisely indicates that he has to flush the buffered writer, which is the solution to the problem.
@Alderath The answer seems to have been edited during the time when I cast the downvote; I'll remove it.
1

You need to close the writer to be sure all the data are written

try{
        writer.write("test log message");
        writer.flush(); // <--- optional
    }catch(Exception e){
        System.err.println("could not write to log");
        e.printStackTrace();
    }

// after you recorded everything, close your writer
finally {
try {
    writer.close();
} catch(IOException ioex) { }
}

Comments

0

You need to call writer.flush() after writing to the buffered stream. Make sure to close the writer when you are done! Try:

...
System.out.println("writing to log");
//Record to log
        try{
            writer.write("test log message");
            writer.flush();
            writer.close(); 
        }catch(Exception e){
            System.err.println("could not write to log");
            e.printStackTrace();
        }

Edit: As per sblundy's answer, in this case a call to close() is sufficient to flush and close the stream. In general you will want to make use of flush() to properly write to the underlying file writer, and close() to close the stream.

Comments

0

Try to add \n symbol to the end of the string.

writer.write("test log message\n");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.