1

I want to write to a file in Java (that I have created succesfully also in Java).
I need to append lines to it and following the online instructions my code should work.
I'm never triggering errors (never do I reach a catch block), but I'm not getting any text in the textfile.
all variables are set and correct.

this is my code atm:

private void handleException () {
    String fullPath = logPath + File.separator + logName;
    File logFile = new File(fullPath);
    if (!logFile.exists()) {
        createLogFile(logFile);
    }
    String log = createLog();
    addLogToFile(fullPath, log);
    /*
    if (Bad error of errorlogs > 20 ofazo) {
        alertAdministrator();
    }
    */
}

private void createLogFile(File logFile) {
    try {
        logFile.getParentFile().mkdirs(); //can cause duplicated files in MacOS
        logFile.createNewFile();
    } catch (IOException ex) {
        Logger.getLogger(ErrorHandeling.class.getName()).log(Level.SEVERE, null, ex);
        alertAdministrator("Error while writing logs.\nErrormessage: " + ex.toString());
    }
}

private String createLog() {
    String log = lineCount + ": " + message + "\n occurred in: file=" + writableStackTrace[0].getFileName() + " class=" + writableStackTrace[0].getClassName() + " method=" + writableStackTrace[0].getMethodName() + " line=" + writableStackTrace[0].getLineNumber() + "\n caused by: " + cause.getMessage();
    return log;
}

private void addLogToFile(String fullPath, String log) {
    try {
        FileWriter fw = new FileWriter(fullPath, true);
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter out = new PrintWriter(bw);
        out.println(log);
    } 
    catch (IOException ex) {
        alertAdministrator("Error while writing logs.\nErrormessage: " + ex.toString());
    }
}
3
  • 4
    Maybe just use a logging framework? I'm pretty sure log4j2 has no issues appending to a file... Commented Mar 27, 2017 at 21:22
  • 5
    Hint: ALWAYS CLOSE YOUR AutoCloseable RESOURCES. Commented Mar 27, 2017 at 21:23
  • Just change the PrintWriter initialization to PrintWriter out = new PrintWriter(bw, true); Commented Mar 27, 2017 at 21:28

3 Answers 3

5

Check the documentation. The constructor you are using:

https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html#PrintWriter(java.io.Writer)

clearly says:

Creates a new PrintWriter, without automatic line flushing.

so don't forget to call the flush() method and close() the writer once done.

https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html#flush() https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html#close()

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

Comments

1

You aren't closing the PrintWriter.

But this is poor design. You should keep the file open, not open and close it for every write.

Comments

0

so don't forget to call the flush() method and close() the writer once done.

Thank you for the solution jlordo, flushing and closing solved it

But this is poor design. You should keep the file open, not open and close it for every write.

Thank you for the info EJP, I'm flushing the file and I'm keeping it open until the program crashes or closes correctly

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.