I'm working with a small project that performs the reading of the data of a spreadsheet, in order to facilitate the capture of the exceptions, I have a class called the Log Generator.
This class has a PrintWriter object that writes the Throwable that it captures and stores in a txt file, this object is instantiated when I start my view and closed when I close it, my intention is to know if it is possible to close write the file and resume it again using the same object at runtime?
My intention is to provide a button that allows the user of the application to see this file, but I'm a bit confused as to how to do this since the file is open for recording throughout the application.
package br.com.layoutbuilder.domain;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
public class GeradorLog {
private PrintWriter out;
public GeradorLog() {
try {
out = new PrintWriter(new FileWriter("LogAplicativo.txt", true));
} catch (IOException e) {
e.printStackTrace();
}
}
public void gravaErro(Throwable erro) {
out.print("Erro ocorrido em: ");
out.println(new Date());
out.print("Mensagem de erro: ");
out.println(erro);
out.print("Stacktrace: ");
erro.printStackTrace(out);
}
public void close() {
if (out != null) {
out.flush();
out.close();
}
}
}