Hi I’m using the below method to write to a file from the Jtextarea and I call this method every 30 second within a Timer but instead to add only new line in file it rewrite the entire lines contained in Jtextarea so then I have duplicate lines. I want to avoid this and update the file just with new lines. Could you help me please.
public void loger() {
FileWriter writer = null;
try {
writer = new FileWriter("MBM_Log_"+date()+".txt" , true);
textArea.write(writer);
} catch (IOException exception) {
System.err.println("log error");
exception.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException exception) {
System.err.println("Error closing writer");
exception.printStackTrace();
}
}
}
}
I changed my code instead to write to the file from the Jtextarea I write the string (LOG) directly to the file. My method loger() become as below :
public void loger (String texLine){
And then then I write the log to the Jtextarea to be displayed in GUI and calling the method loger() to write to the file e.g:
textArea.append(dateTime()+ ” : Alarm sound muted by the Operator from the Menu Bar “);
loger(dateTime()+ ” : Alarm sound muted by the Operator from the Menu Bar “);
By this way I have the logs in Jtextarea and in the file. My problem is resolved when I restart the application the file is not erased and the new log is added to the file.
Thanks to all.