读书人

日记模块

发布时间: 2012-09-09 09:27:54 作者: rapoo

日志模块

????? 下面是一个简单的日志模块,主要功能是用一个TXT文本记录一些异常输出,避免过多的使用system.out.println();,还有就是便于更加彻底的寻找BUG源。

????? package cn.netjava.Logtools;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;

//日志模块
public class Logtools {
?public Logtools(){}
?//记录消息的方法
?public static void info(String msg){
??//将消息写入一个文件中
??//得到当前系统的时间
??Calendar now=Calendar.getInstance();
??String time=now.get(Calendar.YEAR)+"-"+now.get(Calendar.MONTH+1)+"-"+now.get(Calendar.DAY_OF_MONTH)
??+" "+now.get(Calendar.HOUR_OF_DAY)+":"+now.get(Calendar.MINUTE)+":"+now.get(Calendar.SECOND);
??
??File file=new File("D:\\java1\\ManageSystem\\WebRoot\\note.txt");
??if(file.exists()){
???try{
???java.io.BufferedWriter buf=new BufferedWriter(new FileWriter(new File("D:\\java1\\ManageSystem\\WebRoot\\note.txt"),true));
???buf.write(time+"----"+msg+"\r\n");
???buf.flush();
???buf.close();
???}catch(Exception e){
????e.fillInStackTrace();
???}
??}else if(!file.exists()){
???try {
????file.createNewFile();
???} catch (IOException e) {
????e.printStackTrace();
???}
???
??}
?}

}
?????

读书人网 >软件架构设计

热点推荐