java實現寫入日誌類,方便其他類中調用

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

 

package classes;

 

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Date;

import javax.swing.JOptionPane;

 

 

/**

 *

 * @author Administrator

 */

public class AddLog {

 

 public static void addlog(String error)//將錯誤信息寫入日誌

    {

        FileWriter fw=null;

        BufferedWriter bw=null;

        File logFile=null;

        String containt=(new Date()).toString()+"/t/t"+error;

        try {

            logFile = new File("datalog.zhoujian");

            if (!logFile.exists()) {

                logFile.createNewFile();//如果該日誌文件不存在,則新建一個

            }

            fw=new FileWriter(logFile,true);//以追加的模式操作文件

            bw=new BufferedWriter(fw);

bw.newLine();

                        bw.newLine();

bw.write(containt);

bw.close();

                fw.close();

 

        }

        catch (IOException ex) {

            JOptionPane.showMessageDialog(null,"寫入日誌文件出錯!","嚴重警告",JOptionPane.ERROR_MESSAGE);

        }finally{

 

        }

 

 

 

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章