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.
61 lines
1.8 KiB
61 lines
1.8 KiB
package model;
|
|
|
|
import java.io.IOException;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.DirectoryStream;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
* 读取用户文件夹下已有题目的所有题目文本(用于查重)
|
|
*/
|
|
public class LoadFile {
|
|
|
|
public static List<String> loadExistingQuestions(String username) {
|
|
List<String> all = new ArrayList<>();
|
|
Path userDir = Paths.get("data", username);
|
|
if (!Files.exists(userDir)) {
|
|
return all;
|
|
}
|
|
try (DirectoryStream<Path> ds = Files.newDirectoryStream(userDir, "*.txt")) {
|
|
for (Path p : ds) {
|
|
List<String> lines = Files.readAllLines(p, StandardCharsets.UTF_8);
|
|
StringBuilder cur = new StringBuilder();
|
|
for (String line : lines) {
|
|
if (line.matches("^\\s*\\d+\\..*")) {
|
|
if (!cur.isEmpty()) {
|
|
all.add(cur.toString().trim());
|
|
}
|
|
cur.setLength(0);
|
|
cur.append(line.replaceFirst("^\\s*\\d+\\.", "").trim());
|
|
} else {
|
|
if (line.trim().isEmpty()) {
|
|
if (!cur.isEmpty()) {
|
|
all.add(cur.toString().trim());
|
|
cur.setLength(0);
|
|
}
|
|
} else {
|
|
if (!cur.isEmpty()) {
|
|
cur.append(" ");
|
|
}
|
|
cur.append(line.trim());
|
|
}
|
|
}
|
|
}
|
|
if (!cur.isEmpty()) {
|
|
all.add(cur.toString().trim());
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
throw new RuntimeException("读取题目文件失败:" + e.getMessage(), e);
|
|
}
|
|
return all.stream().map(String::trim).filter(s -> !s.isEmpty()).distinct()
|
|
.collect(Collectors.toList());
|
|
}
|
|
}
|