Reputation: 97
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();
}
}
}
}
Upvotes: 1
Views: 1490
Reputation: 97
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){
FileWriter writer = null;
try {
writer = new FileWriter("MBM_Log_"+date()+".txt" , true);
PrintWriter out = new PrintWriter(writer);
out.printf("%s"+"%n", texLine);
out.close();
} 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();
}
}
}
}
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.
Upvotes: 0
Reputation: 8874
You definitely need to append to the file (so leave true
in the constructor).
What you are doing somewhat wrong is to use a Swing component to store data. Swing components are designed to display data. Imagine what would happen if your manager / supervisor / teacher told you to use another GUI library or convert the whole application to a web server - you would have to abandon JTextArea and would then have nowhere to save log messages.
You should have some collection of log messages, for example a List
and then display the messages using this collection. Your log message class can have some function to convert the log message into a String
. Then it would be just a question to go through the list every 30 seconds and append the messages whose timestamp is newer than the time of the last save.
Upvotes: 1
Reputation: 193734
Change:
writer = new FileWriter("MBM_Log_"+Date()+".txt" , true);
to:
writer = new FileWriter("MBM_Log_"+Date()+".txt", false);
or just:
writer = new FileWriter("MBM_Log_"+Date()+".txt");
The constructor you're using for FileWriter
takes two arguments: a String
filename and boolean
which says whether to append to the file if it already exists. As you currently have this set to true
it is appending the contexts of the text area on to the file instead of replacing the file with one with just the current text.
If you want to keep the existing contents of the file either:
Upvotes: 1
Reputation: 25950
writer = new FileWriter("MBM_Log_"+Date()+".txt" , true);
The code above uses appending boolean flag in the constructor, make it false and retry:
writer = new FileWriter("MBM_Log_"+Date()+".txt" , false);
To avoid creating a new file each time, initialize your file writer once outside the method and then use it:
FileWriter writer = new FileWriter("MBM_Log_"+date()+".txt" , true);
public void loger() {
try {
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();
}
}
}
}
Upvotes: 1