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.
math_paper/src/HistoryManager.java

46 lines
1.7 KiB

import java.util.HashSet;
import java.util.Set;
import java.io.*;
public class HistoryManager {
private final Set<String> existingQuestions = new HashSet<>();
public HistoryManager(String username) {
File folder = new File(username);
if (!folder.exists() && !folder.mkdir()) {
System.err.println("无法创建文件夹: " + username);
}
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile() && file.getName().endsWith(".txt")) {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.matches("^\\d+\\..*")) {
int dotIndex = line.indexOf('.');
String question = line.substring(dotIndex + 1).trim();
if (question.endsWith("=")) {
question = question.substring(0, question.length() - 1).trim();
}
existingQuestions.add(question);
}
}
} catch (IOException e) {
System.err.println("读取文件出错: " + file.getName());
}
}
}
}
}
public boolean isDuplicate(String question) {
return existingQuestions.contains(question);
}
public void add(String question) {
existingQuestions.add(question);
}
}