1 /**2 * Generate log files (files are located under Tomcat's installation path)3 * @paramStr4 */5 Public Static voidlogfortxt (String str) {6 Try {7String dir = system.getproperty ("User.dir") + file.separator + "System log";8File fi =NewFile (dir);9 if(!fi.exists ()) {Ten fi.mkdirs (); One } ADate Date =NewDate (); -String time =NewSimpleDateFormat ("Yyyy-mm-dd"). Format (date); -dir = Fi.getpath () + File.separator + time + ". txt"; thefi =NewFile (dir); - if(!fi.exists () &&!fi.isdirectory ()) { - fi.createnewfile (); -PrintWriter PW =NewPrintWriter (NewBufferedWriter (NewFileWriter (FI))); +Pw.println ("" +NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (date) + "Generate log File"); -PW.PRINTLN ("-----------------------------Log-----------------------------"); + pw.println (str); A pw.close (); at}Else { -PrintWriter PW =NewPrintWriter (NewBufferedWriter (NewFileWriter (FI,true))); - pw.println (str); - pw.close (); - } -}Catch(Exception e) { in e.printstacktrace (); - } to}
Java generates TXT log files