前景介绍
将消息存储到 tableStore 失败后,会将数据的相关信息打印到日志文件中,相关信息中包含消息的 id 等信息。我们需要拿到这个日志文件,将存储失败的数据进行重新存储,因此需要拿到从日志文件中的拿到“payload”这个 key 对应的 value 进行消息的重新存储。
如何从一个日志文件中,找到key 对应的 value?
一、先将日志文件进行导入
IO 类的相关知识
链接如下:链接: link
// 利用字节流进行文本出
File file = new File("/Users/hzh/a.txt");
String s = brLine(file);
// 找到分隔符,进行分割后,找到数组中对应的下标位置
String[] split = s.split("\\|");
System.out.println(split[13]);
String json = split[13];
public static String brLine(File file) throws Exception {
// BufferedReader和 FileReader 都是采用了缓存区的,因此 BufferedReader 对于性能的提升不是很大
// 运用到 BufferedReader 和 BufferedWriter 主要是用他们的 readLine()和 nextLine()方法
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String s = bufferedReader.readLine();
return s;
}
二、从中筛选出key 对应的 value
String 类的相关知识
参考 JDK 中 String 相关方法
// 方法一:
// 将 String 转换为 json 对象,然后根据 key 取出。
// 这个的 JsonObject 是 fastjson 包
JSONObject jsonObject = JSONObject.parseObject(json);
String payload = (String)jsonObject.get("opDesc");
System.out.println(payload);
//方法二:
//直接找到对应的下标
String substring = json.substring(27, 37);
System.out.println(substring);