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方法
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 实体类
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 + '\'' +
'}';
}
}