首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

日记模块

2012-09-09 
日志模块????? 下面是一个简单的日志模块,主要功能是用一个TXT文本记录一些异常输出,避免过多的使用system

日志模块

????? 下面是一个简单的日志模块,主要功能是用一个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();
???}
???
??}
?}

}
?????

热点排行