• 自定义日志


    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Date;

    public class WriteLogUtil {

         private static String rootPath = "D:\\logs\\"; 
               
              /**
               * 将信息写到文件中
               * @param msg
               */ 
              @SuppressWarnings("deprecation")
              public static void writeMsgToFile(String msg) { 
                  //删除之前的文件  
                  delOldFile(); 
             
                  FileWriter fileWriter = null; 
                  try { 
                      fileWriter = new FileWriter(getFileName(),true); 
                      Date today = new Date(); 
                      String time = String.valueOf(today.getHours()) + ":" + String.valueOf(today.getMinutes()) + ":" + String.valueOf(today.getSeconds()); 
                      fileWriter.write("#" + time + "# [" + msg + "]" + "\r\n"); 
                      fileWriter.flush(); 
                  } catch (IOException e) { 
                      System.out.println("### 写日志到文件异常 ### >>> " + e.getMessage()); 
                     e.printStackTrace(); 
                  } finally { 
                      try { 
                          fileWriter.close(); 
                      } catch (IOException e) { 
                          System.out.println("### 关闭写日志的流异常 ### >>> " + e.getMessage()); 
                          e.printStackTrace(); 
                      } 
                  } 
              } 
               
              /**
               * 删除之前的日志文件
               */ 
              @SuppressWarnings("deprecation")
              private static void delOldFile() { 
                  Date today = new Date(); 
                  int month = today.getMonth()+1; 
                  month = month - 2; 
                  if(month == -1) month = 11;  //一月份的日志产生則刪除11月份的
                  if(month == 0)  month = 12;  //二月份的日志产生则删除12月份的,其它情况则删除前两个月的日志
                  String delPath = rootPath + String.valueOf(month) + "\\"; 
                  File folder = new File(delPath); 
                  if(folder.exists()) { 
                      File[] files = folder.listFiles(); 
                      for(int i=0; i<files.length; i++) { 
                          files[i].delete(); 
                          System.out.println("删除成功!");
                      } 
                  } 
              } 
               
              /**
               * 获取要保存的文件
               * @return fileName
               */ 
              @SuppressWarnings("deprecation")
              private static String getFileName() { 
                    Date today = new Date(); 
                  String fileName = String.valueOf((today.getYear()+1900)) + String.valueOf((today.getMonth()+1)) + String.valueOf(today.getDate()) + ".log"; 
                   //创建目录  
                  File folder = new File(rootPath + String.valueOf((today.getMonth()+1)) + "\\"); 
                  if(!folder.exists()) { 
                      folder.mkdirs(); 
                  } 
                //创建文件  
                  File file = new File(rootPath + String.valueOf((today.getMonth()+1)) + "\\" +fileName); 
                  if(!file.exists()) { 
                      try { 
                          file.createNewFile();
                          System.out.println("创建文件成功!"+file.getAbsolutePath());
                      } catch (IOException e) { 
                          System.out.println("###新建日志文件异常 ### >>> " + e.getMessage()); 
                          e.printStackTrace(); 
                      } 
                  }
                   
                  fileName = rootPath + String.valueOf((today.getMonth()+1)) + "\\" + fileName; 
                  return fileName; 
              } 
               
              /**
               * 测试使用的main方法
               */ 
              public static void main(String[] args) { 
                 //getFileName();  
                  String testString = "随便写吧"; 
                  writeMsgToFile(testString); 
                  //delOldFile();  
              } 

    }
  • 相关阅读:
    js正则表达语法
    Codeforces 976E/925C (01Trie树)
    ZOJ 3879(大模拟)
    CF967C(二分+细节)
    CF967A(细节模拟)
    HDU 2222(AC自动机模板)
    HDU 5510(KMP+思维)
    HDU 6273(树状数组+思维)
    HDU 6266(思维+规律)
    HDU 6264(思维)
  • 原文地址:https://www.cnblogs.com/beantestng/p/3772831.html
Copyright © 2020-2023  润新知