|
|
|
@ -0,0 +1,109 @@
|
|
|
|
|
// filemanage.java
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.nio.file.FileAlreadyExistsException;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
import java.nio.file.StandardOpenOption;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 文件管理工具类,用于处理用户题目文件。
|
|
|
|
|
*/
|
|
|
|
|
public class FileManager {
|
|
|
|
|
private final List<String> fileLines = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
FileManager fileManager = new FileManager();
|
|
|
|
|
fileManager.createUserFile("张三1", "123");
|
|
|
|
|
fileManager.readUserFiles("张三1");
|
|
|
|
|
for (String line : fileManager.fileLines) {
|
|
|
|
|
System.out.println(line);
|
|
|
|
|
}
|
|
|
|
|
fileManager.writeToFile("张三1", "123", "1");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取用户目录中的所有文件名。
|
|
|
|
|
*/
|
|
|
|
|
public static List<String> getFileNames(String userName) throws IOException {
|
|
|
|
|
Path directoryPath = Path.of("User-problem");
|
|
|
|
|
Path userDirectoryPath = directoryPath.resolve(userName);
|
|
|
|
|
try (var files = Files.list(userDirectoryPath)) {
|
|
|
|
|
return files.filter(Files::isRegularFile)
|
|
|
|
|
.map(path -> path.getFileName().toString())
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 读取指定用户的所有文件内容。
|
|
|
|
|
*/
|
|
|
|
|
public void readUserFiles(String userName) {
|
|
|
|
|
Path directoryPath = Path.of("User-problem");
|
|
|
|
|
Path userDirectoryPath = directoryPath.resolve(userName);
|
|
|
|
|
try {
|
|
|
|
|
List<String> fileNames = getFileNames(userName);
|
|
|
|
|
for (String fileName : fileNames) {
|
|
|
|
|
Path filePath = userDirectoryPath.resolve(fileName);
|
|
|
|
|
try {
|
|
|
|
|
List<String> lines = Files.readAllLines(filePath);
|
|
|
|
|
fileLines.addAll(lines);
|
|
|
|
|
} catch (IOException exception) {
|
|
|
|
|
exception.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException exception) {
|
|
|
|
|
//exception.printStackTrace();
|
|
|
|
|
//没读取到文件时无需处理,稍后新建文件
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 向用户文件中写入内容。
|
|
|
|
|
*/
|
|
|
|
|
public void writeToFile(String userName, String timestamp, String content) {
|
|
|
|
|
List<String> linesToWrite = new ArrayList<>();
|
|
|
|
|
linesToWrite.add(content);
|
|
|
|
|
String fileName = timestamp + ".txt";
|
|
|
|
|
Path directoryPath = Path.of("User-problem");
|
|
|
|
|
Path userDirectoryPath = directoryPath.resolve(userName);
|
|
|
|
|
Path filePath = userDirectoryPath.resolve(fileName);
|
|
|
|
|
try {
|
|
|
|
|
Files.write(filePath, linesToWrite, StandardOpenOption.APPEND);
|
|
|
|
|
} catch (IOException exception) {
|
|
|
|
|
exception.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 为用户创建新文件。
|
|
|
|
|
*/
|
|
|
|
|
public void createUserFile(String userName, String timestamp) {
|
|
|
|
|
String fileName = timestamp + ".txt";
|
|
|
|
|
try {
|
|
|
|
|
Path directoryPath = Paths.get("User-problem");
|
|
|
|
|
if (!Files.exists(directoryPath)) {
|
|
|
|
|
Files.createDirectories(directoryPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Path userDirectoryPath = directoryPath.resolve(userName);
|
|
|
|
|
if (!Files.exists(userDirectoryPath)) {
|
|
|
|
|
Files.createDirectories(userDirectoryPath);
|
|
|
|
|
}
|
|
|
|
|
Path filePath = userDirectoryPath.resolve(fileName);
|
|
|
|
|
Files.createFile(filePath);
|
|
|
|
|
|
|
|
|
|
} catch (FileAlreadyExistsException exception) {
|
|
|
|
|
// 文件已存在,无需操作
|
|
|
|
|
} catch (IOException exception) {
|
|
|
|
|
exception.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<String> getFileLines() {
|
|
|
|
|
return fileLines;
|
|
|
|
|
}
|
|
|
|
|
}
|