zoukankan      html  css  js  c++  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);
        }

    }

  • 相关阅读:
    用C#一次匹配HTML代码中A的链接和文字的方法
    去黑头的7个必胜秘方
    用C#写外挂或辅助工具必须要的WindowsAPI
    用C#把HTML内容转为UBB的方法
    Windows Server 2008 Standard, Enterprise, and Datacenter with Service Pack 2
    xmpp协议分析
    查看Win7的真实版本号方法
    什么是游戏NP?如何在NP下读写游戏内存及如何进入NP进程
    C#正则表达式扫盲 保证 10分钟入门 30分钟精通[绝对不可错过的文章]
    可扩展消息出席协议(XMPP):核心 RFC 3920
  • 原文地址:https://www.cnblogs.com/kungfupanda/p/2605023.html
Copyright © 2011-2022 走看看