ItsShowtime
ItsShowtime

Reputation: 193

Java won't write to existing file using FileWriter

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());
    }
}

Upvotes: 1

Views: 1298

Answers (3)

ItsShowtime
ItsShowtime

Reputation: 193

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

Upvotes: 0

user207421
user207421

Reputation: 311028

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.

Upvotes: 1

jlordo
jlordo

Reputation: 37843

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()

Upvotes: 5

Related Questions