程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 本身寫的java日記類和辦法代碼分享

本身寫的java日記類和辦法代碼分享

編輯:關於JAVA

本身寫的java日記類和辦法代碼分享。本站提示廣大學習愛好者:(本身寫的java日記類和辦法代碼分享)文章只能為提供參考,不一定能成為您想要的結果。以下是本身寫的java日記類和辦法代碼分享正文



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

public class AndyLogger
{
    //The defaulted root path of SSLVPN installation
    private static String rootPath = "C:\\temp2";

    //variable for creating new line
    private final static String enter = System.getProperty("line.separator");

    private static SimpleDateFormat sdf =
        new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    public static synchronized void log(String fileName, String logMessage)
    {
        try
        {
            File folder = new File(rootPath);
            if(!folder.exists())
            {
             folder.mkdir();
            }
            File file = new File(rootPath + "\\" + fileName + ".log");
            if(!file.exists())
            {
             file.createNewFile();
            }
            BufferedReader in = new BufferedReader(new FileReader(file));
            String str = "";
            String strToal = "";

            while ((str = in.readLine()) != null)
            {
                strToal += (str + enter);
            }    
            strToal = strToal + (sdf.format(new Date()) + " " + logMessage + enter);
            in.close();
            BufferedWriter out = new BufferedWriter(new FileWriter(file));
            out.write(strToal);
            out.close();

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

   
    public static synchronized void log(String fileName, String[] logMessages)
    {
        try
        {
            File folder = new File(rootPath);
            if(!folder.exists())
            {
             folder.mkdir();
            }
            File file = new File(rootPath + "\\" + fileName + ".log");
            if(!file.exists())
            {
             file.createNewFile();
            }
            BufferedReader in = new BufferedReader(new FileReader(file));
            String str = "";
            String strToal = "";

            while ((str = in.readLine()) != null)
            {
                strToal += (str + enter);
            }
            for (int i=0; i < logMessages.length ; i++)
            {
               String logMessage = logMessages[i];
               strToal = strToal + (sdf.format(new Date()) + " " + logMessage + enter);
            }
            in.close();
            BufferedWriter out = new BufferedWriter(new FileWriter(file));
            out.write(strToal);
            out.close();

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    public static void main(String args[])
    {
     AndyLogger.log("bug223", "timeisjjja");
     String[] logMessages = {"111","222","333"};
     AndyLogger.log("bug223", logMessages);
    }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved