You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.5 KiB
111 lines
3.5 KiB
package com.example.datacollect.repository;
|
|
|
|
import com.example.datacollect.exception.CrawlerException;
|
|
import com.example.datacollect.exception.ErrorCode;
|
|
import com.example.datacollect.model.Article;
|
|
import java.io.*;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
|
|
public class ArticleRepository {
|
|
private final List<Article> articles = new ArrayList<>();
|
|
private static final String STORAGE_FILE = "articles.dat";
|
|
|
|
public ArticleRepository() {
|
|
loadFromFile();
|
|
}
|
|
|
|
public void add(Article article) {
|
|
if (article == null) {
|
|
throw new IllegalArgumentException("Article cannot be null");
|
|
}
|
|
articles.add(article);
|
|
saveToFile();
|
|
}
|
|
|
|
public void addAll(List<Article> articleList) {
|
|
if (articleList == null) {
|
|
throw new IllegalArgumentException("列表不能为 null");
|
|
}
|
|
for (Article article : articleList) {
|
|
add(article);
|
|
}
|
|
}
|
|
|
|
public List<Article> getAll() {
|
|
return Collections.unmodifiableList(articles);
|
|
}
|
|
|
|
public int size() {
|
|
return articles.size();
|
|
}
|
|
|
|
public void clear() {
|
|
articles.clear();
|
|
saveToFile();
|
|
}
|
|
|
|
private void saveToFile() {
|
|
try (ObjectOutputStream oos = new ObjectOutputStream(
|
|
new FileOutputStream(STORAGE_FILE))) {
|
|
oos.writeObject(new ArrayList<>(articles));
|
|
} catch (IOException e) {
|
|
throw new CrawlerException(ErrorCode.FILE_IO_ERROR, "保存数据到文件失败", e);
|
|
}
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
private void loadFromFile() {
|
|
Path path = Paths.get(STORAGE_FILE);
|
|
if (!Files.exists(path)) {
|
|
return;
|
|
}
|
|
try (ObjectInputStream ois = new ObjectInputStream(
|
|
new FileInputStream(STORAGE_FILE))) {
|
|
List<Article> loaded = (List<Article>) ois.readObject();
|
|
articles.addAll(loaded);
|
|
} catch (IOException | ClassNotFoundException e) {
|
|
throw new CrawlerException(ErrorCode.FILE_IO_ERROR, "从文件加载数据失败", e);
|
|
}
|
|
}
|
|
|
|
public void exportToJson(String filename) {
|
|
StringBuilder json = new StringBuilder();
|
|
json.append("[\n");
|
|
for (int i = 0; i < articles.size(); i++) {
|
|
Article a = articles.get(i);
|
|
json.append(" {\n");
|
|
json.append(" \"title\": \"").append(escapeJson(a.getTitle())).append("\",\n");
|
|
json.append(" \"url\": \"").append(escapeJson(a.getUrl())).append("\",\n");
|
|
json.append(" \"content\": \"").append(escapeJson(a.getContent())).append("\"\n");
|
|
json.append(" }");
|
|
if (i < articles.size() - 1) {
|
|
json.append(",");
|
|
}
|
|
json.append("\n");
|
|
}
|
|
json.append("]");
|
|
|
|
try {
|
|
Files.writeString(Paths.get(filename), json.toString(), StandardCharsets.UTF_8);
|
|
} catch (IOException e) {
|
|
throw new CrawlerException(ErrorCode.FILE_IO_ERROR, "导出JSON失败", e);
|
|
}
|
|
}
|
|
|
|
private String escapeJson(String str) {
|
|
if (str == null) {
|
|
return "";
|
|
}
|
|
return str.replace("\\", "\\\\")
|
|
.replace("\"", "\\\"")
|
|
.replace("\n", "\\n")
|
|
.replace("\r", "\\r")
|
|
.replace("\t", "\\t");
|
|
}
|
|
}
|