forked from francistao/DesignMode_Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogFileOperate.java
More file actions
67 lines (64 loc) · 1.55 KB
/
LogFileOperate.java
File metadata and controls
67 lines (64 loc) · 1.55 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
import java.io.*;
import java.util.*;
/**
* 实现对日志文件的操作
*/
public class LogFileOperate implements LogFileOperateApi{
/**
* 日志文件的路径和文件名称,默认是当前classpath下的AdapterLog.log
*/
private String logFilePathName = "AdapterLog.log";
/**
* 构造方法,传入文件的路径和名称
* @param logFilePathName 文件的路径和名称
*/
public LogFileOperate(String logFilePathName) {
//先判断是否传入了文件的路径和名称,如果是,
//就重新设置操作的日志文件的路径和名称
if(logFilePathName!=null && logFilePathName.trim().length()>0){
this.logFilePathName = logFilePathName;
}
}
public List<LogModel> readLogFile() {
List<LogModel> list = null;
ObjectInputStream oin = null;
try {
File f = new File(logFilePathName);
if(f.exists()){
oin = new ObjectInputStream(
new BufferedInputStream(new FileInputStream(f))
);
list = (List<LogModel>)oin.readObject();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(oin!=null){
oin.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
public void writeLogFile(List<LogModel> list){
File f = new File(logFilePathName);
ObjectOutputStream oout = null;
try {
oout = new ObjectOutputStream(
new BufferedOutputStream(new FileOutputStream(f))
);
oout.writeObject(list);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
oout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}