国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

使用java實現日志工具類分享

2019-11-26 15:36:52
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

package com.teligen.eos.teleCode;

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
  */
 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();
   }
  }
 }

 /**
  * 刪除之前的日志文件
  */
 private static void delOldFile() {
  Date today = new Date();
  int month = today.getMonth()+1;
  month = month - 2;
  if(month == -1) month = 11;
  if(month == 0) month = 12;
  String delPath = rootPath + String.valueOf(month) + "http://";
  File folder = new File(delPath);
  if(folder.exists()) {
   File[] files = folder.listFiles();
   for(int i=0; i<files.length; i++) {
    files[i].delete();
   }
  }
 }

 /**
  * 獲取要保存的文件
  * @return fileName
  */
 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)) + "http://");
  if(!folder.exists()) {
   folder.mkdirs();
  }
  //創建文件
  File file = new File(fileName);
  if(!file.exists()) {
   try {
    file.createNewFile();
   } catch (IOException e) {
    System.out.println("### 新建日志文件異常 ### >>> " + e.getMessage());
    e.printStackTrace();
   }
  }

  fileName = rootPath + String.valueOf((today.getMonth()+1)) + "http://" + fileName;

  return fileName;
 }

 /**
  * 測試使用的main方法
  */
 public static void main(String[] args) {
  //getFileName();
  String testString = "寫日志咯:71FABB7890D2CC0D267FBD84F409618C0303BC597B9244C324947BDE4B1C0B4CB08C33FC461F7BADD088535DAE42D8D7D06F4134E442D9D1CE3A0F9B3EDD64337A2D18CE34FCDC137B7CBD84F409618C03038FEAEC79F79C2F58BD84F409618C03038FEAEC79F79C2F58BD84F409618C03038FEAEC79F79C2F581790ACB3C178641D14D8C09905BC52CF1C8249B12F2EDE5AC3C8FAF2FD8A686E";
  writeMsgToFile(testString);
  //delOldFile();
 }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 湾仔区| 个旧市| 浦城县| 德庆县| 洪泽县| 来安县| 云南省| 铜川市| 重庆市| 临清市| 湘西| 正定县| 双柏县| 伊金霍洛旗| 舞阳县| 阿克陶县| 醴陵市| 仪陇县| 吉木萨尔县| 新龙县| 华宁县| 竹山县| 尚志市| 河西区| 香河县| 衡水市| 韶山市| 缙云县| 罗城| 沈丘县| 兴文县| 琼结县| 巴塘县| 苗栗市| 理塘县| 阳西县| 吴江市| 锡林浩特市| 锦屏县| 柞水县| 南漳县|