I am writing an app in Java (new developer) and I am trying to save messages and things in log files. (Log / [date] .txt).
on public zeros message (string channel, string nick, string account, string hostname, string message) {simple data formatting sdf = new simpledetform ("hh: mm: ss dd / mm / yay") ; SimpleDateform SDFD = New SimpleDateform ("DD / MM / YYYA"); Date now = new date (); Try {/ * check that the log folder is present / / file logdir = new file ("log /"); Boolean results = true; If (! Logdir.exists ()) {try {logdir.mkdir ()}; } Hold (Exception e) {System.err.println ("[Error] Directory 'Could not log' / '';); Results = Incorrect;}} / * Check whether the log file exists / file file = new File ("log /" + sdfd.format (now). Replace ("/", "-" + ".txt"); if (! Fcheck.exists ()) {try {fcheck.createNewFile ()}} hold (Exception e) {System.err.println ("[ERROR] Could not create log file" log / "+ sdfd.format (now) .replace (" / "," - ") +" .txt '"); }} / * (/ Result if) {FileWriter file = new filerurder ("log /" + sdfd.format (now). / Replace ("/", "-" + ".txt"); PrintWriter writes in / Are = new printers (files); Triangle Entry = "[" + sdf.format (now) + "] [" + + + "]" + Nick + "(" + account + ")>" + message + "\ n"; write. Append (write); write.close (); file.close ();} Else {System.err.println ("[error] the line could not be saved in the log file.");}} Hold (exception e) {System.err.println ("[Error] The line for the log file could not be saved.");}}
Sorry if it is not surprisingly obvious, but I'm still learning Java as you can see, I have write.append (entry);
- which I thought would be attached to my log file, thus allowing me to save and save and keep all entries.
You are closing PrintWriter
on each entry - call each time A new example is created to overwrite that method and file.
If you do not want to change this, you can set FileWriter
to app mode .
fiver file = new flumerwriter ("log /" + sdfd.format (now). Replace ("/", "-") + ".txt", is true); PrintWriter writes = new printItter (file);