FileUtils.java
2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.sincere.haikang.utils;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 日志记录、文件操作工具类
*/
public class FileUtils {
private static FileUtils fileUtils;
private String filePath = "./log/";//日志记录目录
public static String devices = "devices.txt";//设备记录
public static String sendUserErrTxt = "senduserErr.txt";//用户下发失败记录
public static String sendUserSucTxt = "senduserSuc.txt";//用户下发成功记录
public static String qiandaoSuccess = "qiandaoSuccess.txt";//用户签到成功记录
public static String baojingneirong = "baojingneirong.txt";//报警内容
public static String qiandaoErr = "qiandaoErr.txt";//用户签到失败记录
public static String qinshiResult = "寝室签到接口记录.txt";//用户签到失败记录
public static String sendNodevice = "没有设备.txt";//设备没有
public static String schoolId = "学校id记录.txt";//学校id记录表
public static String zhiwen = "指纹签到.txt";//学校id记录表
public static String yichangtime = "上报异常时间.txt";//学校id记录表
public static String liantong = "联通短信.txt";//学校id记录表
public static FileUtils getInstance() {
if (null == fileUtils) {
synchronized (FileUtils.class) {
fileUtils = new FileUtils();
}
}
return fileUtils;
}
public FileUtils() {
File filePa = new File(filePath);
if (!filePa.exists()) filePa.mkdirs();
}
/**
* @param content 日志内容
* @param fileName 文件名字
*/
public void writeLogs(String content, String fileName) {
String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
File path = new File(filePath+datePath+"/");
if (!path.exists())path.mkdirs();
String time = new SimpleDateFormat("HH").format(new Date());
File logPath = new File(path, time+"_"+fileName);
try {
// System.out.println("logPath:" + logPath.getAbsolutePath());
if (!logPath.exists()) logPath.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(logPath, true);//true表示文件后面续写
String writeContent = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
+ " " + content + "\r\n";
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
outputStreamWriter.write(writeContent);
outputStreamWriter.write("\r\n");
outputStreamWriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}