修改生成的数据文件名称

This commit is contained in:
xuelijun
2026-01-23 10:42:36 +08:00
parent ba9113afc2
commit 87b2ea6a11
3 changed files with 20 additions and 4 deletions

BIN
bocai.db

Binary file not shown.

View File

@@ -26,7 +26,7 @@ public class CrawlerSchedule {
// 每天凌晨2点执行爬取开奖结果
//@Scheduled(cron = "0 0 2 * * ?")
// 每7秒执行一次爬取开奖结果
//@Scheduled(cron = "*/9 * * * * ?")
@Scheduled(cron = "*/9 * * * * ?")
public void executeLotteryDraw() {
System.out.println("开始爬取开奖结果...");
int retryCount = 0;

View File

@@ -12,6 +12,7 @@ import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -246,9 +247,13 @@ public class LotteryWebMagicCrawler implements PageProcessor {
// 设置 JSON 格式化(可选,更易读)
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
// 定义输出目录和文件名
// 定义输出目录
String directoryPath = "output/json"; // 项目根目录下的 output/json 文件夹
String fileName = "result_" + System.currentTimeMillis() + ".json";
// 使用年月日作为文件名格式result_yyyyMMdd.json
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
String dateStr = dateFormat.format(new Date());
String fileName = "result_" + dateStr + ".json";
String filePath = directoryPath + "/" + fileName;
// 创建目录(如果不存在)
@@ -256,8 +261,19 @@ public class LotteryWebMagicCrawler implements PageProcessor {
if (!directory.exists()) {
directory.mkdirs(); // 创建多级目录
}
// 创建文件对象
File outputFile = new File(filePath);
// 如果文件已存在,删除旧文件(实现替换功能)
if (outputFile.exists()) {
boolean deleted = outputFile.delete();
if (!deleted) {
throw new IOException("无法删除已存在的文件: " + filePath);
}
System.out.println("已删除旧文件,准备创建新文件: " + fileName);
}
// 将 List 写入 JSON 文件
objectMapper.writeValue(outputFile, resultList);
System.out.println("数据已成功写入文件: " + outputFile.getAbsolutePath());