修改时间1
This commit is contained in:
@@ -508,9 +508,52 @@ public class HttpClientExample {
|
||||
if (!directory.exists()) {
|
||||
directory.mkdirs(); // 创建多级目录
|
||||
}
|
||||
|
||||
// 创建文件对象
|
||||
File outputFile = new File(filePath);
|
||||
// 如果文件已存在,读取现有数据并对比
|
||||
List<Map<String, Object>> existingData = new ArrayList<>();
|
||||
Set<String> existingIds = new HashSet<>();
|
||||
if (outputFile.exists()) {
|
||||
try {
|
||||
existingData = objectMapper.readValue(outputFile,
|
||||
objectMapper.getTypeFactory().constructCollectionType(List.class, Map.class));
|
||||
for (Map<String, Object> item : existingData) {
|
||||
if (item.containsKey("id")) {
|
||||
existingIds.add(item.get("id").toString());
|
||||
}
|
||||
}
|
||||
log.info("已读取现有数据,共 " + existingData.size() + " 条记录");
|
||||
} catch (IOException e) {
|
||||
log.warn("读取现有文件失败,将覆盖写入: " + e.getMessage());
|
||||
existingIds.clear();
|
||||
}
|
||||
}
|
||||
|
||||
// 筛选出新增的数据(id不在existingIds中的记录)
|
||||
List<Map<String, Object>> newData = new ArrayList<>();
|
||||
for (Map<String, Object> item : resultList) {
|
||||
if (item.containsKey("id")) {
|
||||
String id = item.get("id").toString();
|
||||
if (!existingIds.contains(id)) {
|
||||
newData.add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 合并现有数据和新数据
|
||||
List<Map<String, Object>> finalData = new ArrayList<>();
|
||||
if (!existingData.isEmpty()) {
|
||||
finalData.addAll(existingData);
|
||||
}
|
||||
finalData.addAll(newData);
|
||||
|
||||
// 将合并后的数据写入 JSON 文件
|
||||
objectMapper.writeValue(outputFile, finalData);
|
||||
log.info("数据已成功写入文件: " + outputFile.getAbsolutePath() +
|
||||
" (现有: " + existingData.size() + " 条, 新增: " + newData.size() + " 条, 总计: " + finalData.size() + " 条)");
|
||||
|
||||
/* // 创建文件对象
|
||||
File outputFile = new File(filePath);
|
||||
|
||||
// 如果文件已存在,删除旧文件(实现替换功能)
|
||||
if (outputFile.exists()) {
|
||||
@@ -523,7 +566,7 @@ public class HttpClientExample {
|
||||
|
||||
// 将 List 写入 JSON 文件
|
||||
objectMapper.writeValue(outputFile, resultList);
|
||||
log.info("数据已成功写入文件: " + outputFile.getAbsolutePath());
|
||||
log.info("数据已成功写入文件: " + outputFile.getAbsolutePath());*/
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
log.error("写入 JSON 文件失败: " + e.getMessage(), e);
|
||||
|
||||
Reference in New Issue
Block a user