package com.project.storage; import com.project.model.PostInfo; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; public class DataStorage { public static void saveToCSV(List posts, String directory) { if (posts == null || posts.isEmpty()) { System.out.println("没有数据需要保存"); return; } try { java.nio.file.Path dirPath = Paths.get(directory); if (!Files.exists(dirPath)) { Files.createDirectories(dirPath); } String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")); String filename = "posts_" + timestamp + ".csv"; java.nio.file.Path filePath = dirPath.resolve(filename); try (BufferedWriter writer = new BufferedWriter( new FileWriter(filePath.toFile(), StandardCharsets.UTF_8))) { writer.write("\uFEFF"); writer.write("标题,内容,作者,发布日期,点赞数,评论数,浏览量,标签,情感倾向\n"); for (PostInfo post : posts) { writer.write(post.toCSV()); writer.write("\n"); } } System.out.println("数据已保存到: " + filePath.toAbsolutePath()); } catch (IOException e) { System.err.println("保存CSV文件时出错: " + e.getMessage()); } } public static void saveToJSON(List posts, String directory) { if (posts == null || posts.isEmpty()) { System.out.println("没有数据需要保存"); return; } try { java.nio.file.Path dirPath = Paths.get(directory); if (!Files.exists(dirPath)) { Files.createDirectories(dirPath); } String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")); String filename = "posts_" + timestamp + ".json"; java.nio.file.Path filePath = dirPath.resolve(filename); try (BufferedWriter writer = new BufferedWriter( new FileWriter(filePath.toFile(), StandardCharsets.UTF_8))) { writer.write("[\n"); for (int i = 0; i < posts.size(); i++) { writer.write(postToJSON(posts.get(i))); if (i < posts.size() - 1) { writer.write(",\n"); } else { writer.write("\n"); } } writer.write("]\n"); } System.out.println("数据已保存到: " + filePath.toAbsolutePath()); } catch (IOException e) { System.err.println("保存JSON文件时出错: " + e.getMessage()); } } private static String postToJSON(PostInfo post) { return String.format( " {\n" + " \"title\": \"%s\",\n" + " \"content\": \"%s\",\n" + " \"author\": \"%s\",\n" + " \"postDate\": \"%s\",\n" + " \"likeCount\": %d,\n" + " \"commentCount\": %d,\n" + " \"viewCount\": %d,\n" + " \"tags\": \"%s\",\n" + " \"sentiment\": \"%s\"\n" + " }", escapeJSON(post.getTitle()), escapeJSON(post.getContent()), escapeJSON(post.getAuthor()), post.getPostDate() != null ? post.getPostDate().toString() : "", post.getLikeCount(), post.getCommentCount(), post.getViewCount(), escapeJSON(post.getTags()), escapeJSON(post.getSentiment()) ); } private static String escapeJSON(String text) { if (text == null) { return ""; } return text.replace("\\", "\\\\") .replace("\"", "\\\"") .replace("\n", "\\n") .replace("\r", "\\r") .replace("\t", "\\t"); } }