Background:
This afternoon, when I write a multi-threaded test program reader writer-writer first (this article will be published later, I want to see the printing result of the console, but you know, the eclipse console can only save part of the data. Log4j configuration is a little tricky.
So I wrote this tool class that can write local files myself:
Functions implemented by the tool class:
1. Follow[Time-call class-call method-row number of the call method-message]Press enter to write the file
2. One log file every day
If you don't talk about it, go to the Code:
Write2file. Java
Package COM. bankht. writerreader; import Java. io. file; import Java. io. filewriter; import Java. io. ioexception; import Java. util. calendar; import Java. util. date; import Java. util. gregoriancalendar;/*** @ Author: zhuyong * @ Creation Time: 03:10:30, December 7, ** @ Class description: write File */public class write2file {Private Static final string gettoday = new gregoriancalendar (). get (calendar. year) + "-" + new gregoriancalendar (). get (calendar. month) + "-" + new gregoriancalendar (). get (calendar. day_of_month) + "-"; Private Static final string filepath = "D:/" + gettoday + "log.txt"; Public void write2file (string message) {stacktraceelement stack [] = (New throwable ()). getstacktrace (); stacktraceelement S = stack [1]; string headermessage = S. getclassname () + ". "+ S. getmethodname () + "()" +"★Linenum: "+ S. getlinenumber () +"★Message: "; headermessage = adddatetimeheader (headermessage); message = headermessage + message +" \ r \ n "; filewriter = NULL; file = NULL; try {file = new file (filepath); filewriter = new filewriter (file, true); filewriter. write (Message);} catch (ioexception IE) {IE. printstacktrace ();} finally {try {filewriter. close ();} catch (ioexception IE) {IE. printstacktrace () ;}} Public String adddatetimeheader (string headermessage) {string datetimeheader = new date (). tolocalestring () +"★"; Return datetimeheader + = headermessage ;}}
Testwrite2file. Java
Package COM. bankht. writerreader; import Org. JUnit. test;/*** @ Author: zhuyong * @ Creation Time: 03:30:54 ** @ Class description: test File writing */public class testwrite2file {private write2file log = new write2file (); Private string message = NULL; @ testpublic void testwrite2file1 () {message = "ak- AK47 "; log. write2file (Message) ;}@ testpublic void testwrite2file2 () {message = "currently focused on cloud computing and agile development"; log. write2file (Message );}}
Display Effect:
File display:
17:26:02★Com. bankht. writerreader. testwrite2file. testwrite2file1 ()★Linenum: 18★Message: Special Forces-AK472012-6-7 17:26:05★Com. bankht. writerreader. testwrite2file. testwrite2file2 ()★Linenum: 23★Message: currently focuses on cloud computing and agile development 17:30:21★Com. bankht. writerreader. testwrite2file. testwrite2file2 ()★Linenum: 23★Message: currently focuses on cloud computing and agile development 17:30:39★Com. bankht. writerreader. testwrite2file. testwrite2file1 ()★Linenum: 18★Message: Special Forces-AK472012-6-7 17:31:50★Com. bankht. writerreader. testwrite2file. testwrite2file2 ()★Linenum: 23★Message: currently focuses on cloud computing and agile development 17:36:13★Com. bankht. writerreader. testwrite2file. testwrite2file1 ()★Linenum: 19★Message: Special Forces-AK472012-6-7 17:45:46★Com. bankht. writerreader. testwrite2file. testwrite2file2 ()★Linenum: 25★Message: currently focuses on cloud computing and agile development