发布于 2016-07-10 13:05:11 | 172 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Java并发编程示例,程序狗速度看过来!

Java程序设计语言

java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。


这篇文章主要介绍的Java代码工具类是用于书写日志信息到指定的文件,并且具有删除之前日志文件的功能,需要的朋友可以参考下


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) + "\\";
  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)) + "\\");
  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)) + "\\" + fileName;

  return fileName;
 }

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务