FileUtils.java
2.59 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
package com.sincere.server1;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 日志记录、文件操作工具类
*/
public class FileUtils {
public static String checkFail = "检测失败.txt";
public static String checkSuc = "检测成功.txt";
public static String device_login="设备登录id.txt";
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 qiandaoErr = "qiandaoErr.txt";//用户签到失败记录
public static String sendNodevice = "没有设备.txt";//设备没有
public static String sendOrder = "下发指令.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 date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
File path = new File(filePath+date);
if (!path.exists())path.mkdirs();
File logPath = new File(filePath+date, 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();
}
}
}