diff --git a/src/main/java/com/example/exam/exam/service/autoForWinEdgeSetting/AutoForWinEdgeSettingServiceImpl.java b/src/main/java/com/example/exam/exam/service/autoForWinEdgeSetting/AutoForWinEdgeSettingServiceImpl.java index bcad3dc..7121e4e 100644 --- a/src/main/java/com/example/exam/exam/service/autoForWinEdgeSetting/AutoForWinEdgeSettingServiceImpl.java +++ b/src/main/java/com/example/exam/exam/service/autoForWinEdgeSetting/AutoForWinEdgeSettingServiceImpl.java @@ -19,9 +19,7 @@ import jakarta.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; +import java.io.*; import java.math.BigDecimal; import java.util.*; @@ -274,25 +272,30 @@ public class AutoForWinEdgeSettingServiceImpl implements AutoForWinEdgeSettingSe File file = new File(filePath); try { - // 如果文件存在且文件不为空,尝试读取现有内容 - Map existingMap = null; + Map existingMap = new HashMap<>(); + if (file.exists() && file.length() > 0) { - existingMap = objectMapper.readValue(file, Map.class); + try (InputStream fis = new FileInputStream(file)) { + existingMap = objectMapper.readValue(fis, Map.class); + } } - // 如果文件为空或无法读取,初始化为空的Map - if (existingMap == null) { - existingMap = new HashMap<>(); - } - - // 合并新的Map数据 existingMap.putAll(map); - // 将合并后的Map写入到文件 - objectMapper.writeValue(file, existingMap); + try (OutputStream fos = new FileOutputStream(file, false); + BufferedOutputStream bos = new BufferedOutputStream(fos)) { + objectMapper.writeValue(bos, existingMap); + bos.flush(); // 强制刷新 + } + + // 可选占用测试 + boolean released = file.renameTo(file); + System.out.println("文件释放状态: " + released); + } catch (IOException e) { e.printStackTrace(); } + return "学生答案已经写入到 " + filePath; } diff --git a/src/main/java/com/example/exam/exam/service/autoforchoice/AutoForChoiceServiceImpl.java b/src/main/java/com/example/exam/exam/service/autoforchoice/AutoForChoiceServiceImpl.java index 3a53107..e2e7f19 100644 --- a/src/main/java/com/example/exam/exam/service/autoforchoice/AutoForChoiceServiceImpl.java +++ b/src/main/java/com/example/exam/exam/service/autoforchoice/AutoForChoiceServiceImpl.java @@ -12,13 +12,12 @@ import com.example.exam.exam.service.tenant.SystemTenantService; import com.example.exam.exam.utils.c.LogFileUtils; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; import jakarta.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; +import java.io.*; import java.math.BigDecimal; import java.util.*; @@ -282,29 +281,35 @@ public class AutoForChoiceServiceImpl implements AutoForChoiceService { File file = new File(filePath); try { - // 如果文件存在且文件不为空,尝试读取现有内容 - Map existingMap = null; + Map existingMap = new HashMap<>(); + if (file.exists() && file.length() > 0) { - existingMap = objectMapper.readValue(file, Map.class); + try (InputStream fis = new FileInputStream(file)) { + existingMap = objectMapper.readValue(fis, Map.class); + } } - // 如果文件为空或无法读取,初始化为空的Map - if (existingMap == null) { - existingMap = new HashMap<>(); - } - - // 合并新的Map数据 existingMap.putAll(map); - // 将合并后的Map写入到文件 - objectMapper.writeValue(file, existingMap); + try (OutputStream fos = new FileOutputStream(file, false); + BufferedOutputStream bos = new BufferedOutputStream(fos)) { + objectMapper.writeValue(bos, existingMap); + bos.flush(); // 强制刷新 + } + + // 可选占用测试 + boolean released = file.renameTo(file); + System.out.println("文件释放状态: " + released); + } catch (IOException e) { e.printStackTrace(); } + return "学生答案已经写入到 " + filePath; } + public static String convertToLetter(int number) { // 判断是否在 1 到 26 的范围内 if (number >= 1 && number <= 26) { diff --git a/src/main/java/com/example/exam/exam/utils/c/LogFileUtils.java b/src/main/java/com/example/exam/exam/utils/c/LogFileUtils.java index 8b84054..2506c0f 100644 --- a/src/main/java/com/example/exam/exam/utils/c/LogFileUtils.java +++ b/src/main/java/com/example/exam/exam/utils/c/LogFileUtils.java @@ -29,14 +29,18 @@ public class LogFileUtils { file.getParentFile().mkdirs(); // 创建父目录 file.createNewFile(); } - writer = new BufferedWriter(new FileWriter(file, true)); // 追加写入模式 + + try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, true))) { + writer.write(""); // 可选写入内容 + } + } catch (IOException e) { - log.error(e.getMessage()); - throw new RuntimeException("无法创建日志文件: " + e.getMessage()); + throw new RuntimeException("无法创建日志文件: " + e.getMessage(), e); } } + /** * 写入文本,自动换行 * @param content 写入的文本