java读取文件-> 转换成Object

java读取文件-> 转换成Object

Java8 新特性

文本内容

2015-12-25 10:39:29 INFO ApiOriginFilter.java {“appVersion”:”3.5.0”,”channels”:”AppStore”,”requestDate”:”2015-12-25 10:39:29”,”deviceModel”:”iPad”,”deviceVersion”:”ios_8.4”,”userId”:”1”,”platform”:”16”,”deviceUUID”:”498104EF5E64BB9D46BDA437E2254CBB”}
2015-12-25 10:39:39 INFO ApiOriginFilter.java {“appVersion”:”3.5.0”,”channels”:”AppStore”,”requestDate”:”2015-12-25 10:39:39”,”deviceModel”:”iPad”,”deviceVersion”:”ios_8.4”,”userId”:”1”,”platform”:”16”,”deviceUUID”:”498104EF5E64BB9D46BDA437E2254CBB”}
2015-12-25 10:39:41 INFO ApiOriginFilter.java {“appVersion”:”3.5.0”,”channels”:”AppStore”,”requestDate”:”2015-12-25 10:39:41”,”deviceModel”:”iPad”,”deviceVersion”:”ios_8.4”,”userId”:”1”,”platform”:”16”,”deviceUUID”:”498104EF5E64BB9D46BDA437E2254CBB”}
2015-12-25 10:39:42 INFO ApiOriginFilter.java {“appVersion”:”3.5.0”,”channels”:”AppStore”,”requestDate”:”2015-12-25 10:39:42”,”deviceModel”:”iPad”,”deviceVersion”:”ios_8.4”,”userId”:”1”,”platform”:”16”,”deviceUUID”:”498104EF5E64BB9D46BDA437E2254CBB”}
2015-12-25 10:39:43 INFO ApiOriginFilter.java {“appVersion”:”3.5.0”,”channels”:”AppStore”,”requestDate”:”2015-12-25 10:39:43”,”deviceModel”:”iPad”,”deviceVersion”:”ios_8.4”,”userId”:”1”,”platform”:”16”,”deviceUUID”:”498104EF5E64BB9D46BDA437E2254CBB””}

测试 Main方法

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
import com.google.gson.Gson;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public static void main(String[] args) throws IOException {
Gson gson = new Gson();
List<String> lines = Files.readAllLines(Paths.get("/Users/songlijun/Work/Dev/VcPunkinService_api.log"), StandardCharsets.UTF_8);

List<ApiLog> apiLogs = new ArrayList<>();

for (String line : lines) {
// System.out.println(line.substring(line.indexOf("{"), line.length()));
// gson json转换成object
ApiLog apiLog = gson.fromJson(line.substring(line.indexOf("{"), line.length()), ApiLog.class);
apiLogs.add(apiLog);
}

// java8 lambda表达式
apiLogs.forEach(System.out::println);


}

ApiLog 实体类

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
class ApiLog {


private String appVersion;
private String channels;
private String requestDate;
private String deviceModel;
private String deviceVersion;
private String userId;
private String platform;
private String deviceUUID;

public String getAppVersion() {
return appVersion;
}

public void setAppVersion(String appVersion) {
this.appVersion = appVersion;
}

public String getChannels() {
return channels;
}

public void setChannels(String channels) {
this.channels = channels;
}


public String getRequestDate() {
return requestDate;
}

public void setRequestDate(String requestDate) {
this.requestDate = requestDate;
}

public String getDeviceModel() {
return deviceModel;
}

public void setDeviceModel(String deviceModel) {
this.deviceModel = deviceModel;
}

public String getDeviceVersion() {
return deviceVersion;
}

public void setDeviceVersion(String deviceVersion) {
this.deviceVersion = deviceVersion;
}

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String getPlatform() {
return platform;
}

public void setPlatform(String platform) {
this.platform = platform;
}

public String getDeviceUUID() {
return deviceUUID;
}

public void setDeviceUUID(String deviceUUID) {
this.deviceUUID = deviceUUID;
}

@Override
public String toString() {
return "ApiLog{" +
"appVersion='" + appVersion + '\'' +
", channels='" + channels + '\'' +
", requestDate='" + requestDate + '\'' +
", deviceModel='" + deviceModel + '\'' +
", deviceVersion='" + deviceVersion + '\'' +
", userId='" + userId + '\'' +
", platform='" + platform + '\'' +
", deviceUUID='" + deviceUUID + '\'' +
'}';
}
}
LijunSong wechat
欢迎您扫一扫上面的微信公众号,订阅IT江湖!