java保存日誌到本地文本文件

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jcoapp;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *
 * @author luolai
 */
public class LogInfo {

    public static void main(String[] args) {
        String newLog = " Date:" + new Date() + "  |";
        appendLog(newLog);
    }
    
    public static String getCurrentDate() {
        SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sm.format(new Date());
    }

    public static String strRight(String value) {
        return value.substring(value.length() - 2, value.length());
    }

    public static void appendLog(String newLog) {
        Scanner sc = null;
        PrintWriter pw = null;
        Calendar c = new GregorianCalendar();
        File log = new File("log\\loginfo" + String.valueOf(c.get(c.YEAR))
                + strRight("00" + String.valueOf(c.get(c.MONTH)+1)) + strRight("00" + String.valueOf(c.get(c.DAY_OF_MONTH))) + ".log");
        try {
            if (!log.exists())//如果文件不存在,則新建.
            {
                File parentDir = new File(log.getParent());
                if (!parentDir.exists())//如果所在目錄不存在,則新建.
                {
                    parentDir.mkdirs();
                }
                log.createNewFile();
            }
            sc = new Scanner(log);
            StringBuilder sb = new StringBuilder();
            while (sc.hasNextLine())//先讀出舊文件內容,並暫存sb中;
            {
                sb.append(sc.nextLine());
                sb.append("\r\n");//換行符作爲間隔,掃描器讀不出來,因此要自己添加.
            }
            sc.close();

            pw = new PrintWriter(new FileWriter(log), true);
            /*
             * A.
             */
            pw.println(sb.toString());//,寫入舊文件內容.
   /*
             * B.
             */
            pw.println(newLog + "  [" + getCurrentDate() + "]");//寫入新日誌.
   /*
             * 如果先寫入A,最近日誌在文件最後. 如是先寫入B,最近日誌在文件最前.
             */
            pw.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public static void appendLog(String logFileName,String newLog) {
        Scanner sc = null;
        PrintWriter pw = null;
        Calendar c = new GregorianCalendar();
        File log = new File("log\\"+logFileName + String.valueOf(c.get(c.YEAR))
                + strRight("00" + String.valueOf(c.get(c.MONTH)+1)) + strRight("00" + String.valueOf(c.get(c.DAY_OF_MONTH))) + ".log");
        try {
            if (!log.exists())//如果文件不存在,則新建.
            {
                File parentDir = new File(log.getParent());
                if (!parentDir.exists())//如果所在目錄不存在,則新建.
                {
                    parentDir.mkdirs();
                }
                log.createNewFile();
            }
            sc = new Scanner(log);
            StringBuilder sb = new StringBuilder();
            while (sc.hasNextLine())//先讀出舊文件內容,並暫存sb中;
            {
                sb.append(sc.nextLine());
                sb.append("\r\n");//換行符作爲間隔,掃描器讀不出來,因此要自己添加.
            }
            sc.close();

            pw = new PrintWriter(new FileWriter(log), true);
            /*
             * A.
             */
            pw.println(sb.toString());//,寫入舊文件內容.
   /*
             * B.
             */
            pw.println(newLog + "  [" + getCurrentDate() + "]");//寫入新日誌.
   /*
             * 如果先寫入A,最近日誌在文件最後. 如是先寫入B,最近日誌在文件最前.
             */
            pw.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

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