應用java完成日記對象類分享。本站提示廣大學習愛好者:(應用java完成日記對象類分享)文章只能為提供參考,不一定能成為您想要的結果。以下是應用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();
}
}