【修改】 生成json提示被占用问题
This commit is contained in:
@@ -19,9 +19,7 @@ import jakarta.annotation.Resource;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@@ -274,25 +272,30 @@ public class AutoForWinEdgeSettingServiceImpl implements AutoForWinEdgeSettingSe
|
|||||||
File file = new File(filePath);
|
File file = new File(filePath);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 如果文件存在且文件不为空,尝试读取现有内容
|
Map<String, String> existingMap = new HashMap<>();
|
||||||
Map<String, String> existingMap = null;
|
|
||||||
if (file.exists() && file.length() > 0) {
|
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);
|
existingMap.putAll(map);
|
||||||
|
|
||||||
// 将合并后的Map写入到文件
|
try (OutputStream fos = new FileOutputStream(file, false);
|
||||||
objectMapper.writeValue(file, existingMap);
|
BufferedOutputStream bos = new BufferedOutputStream(fos)) {
|
||||||
|
objectMapper.writeValue(bos, existingMap);
|
||||||
|
bos.flush(); // 强制刷新
|
||||||
|
}
|
||||||
|
|
||||||
|
// 可选占用测试
|
||||||
|
boolean released = file.renameTo(file);
|
||||||
|
System.out.println("文件释放状态: " + released);
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
return "学生答案已经写入到 " + filePath;
|
return "学生答案已经写入到 " + filePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -12,13 +12,12 @@ import com.example.exam.exam.service.tenant.SystemTenantService;
|
|||||||
import com.example.exam.exam.utils.c.LogFileUtils;
|
import com.example.exam.exam.utils.c.LogFileUtils;
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@@ -282,29 +281,35 @@ public class AutoForChoiceServiceImpl implements AutoForChoiceService {
|
|||||||
File file = new File(filePath);
|
File file = new File(filePath);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 如果文件存在且文件不为空,尝试读取现有内容
|
Map<String, String> existingMap = new HashMap<>();
|
||||||
Map<String, String> existingMap = null;
|
|
||||||
if (file.exists() && file.length() > 0) {
|
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);
|
existingMap.putAll(map);
|
||||||
|
|
||||||
// 将合并后的Map写入到文件
|
try (OutputStream fos = new FileOutputStream(file, false);
|
||||||
objectMapper.writeValue(file, existingMap);
|
BufferedOutputStream bos = new BufferedOutputStream(fos)) {
|
||||||
|
objectMapper.writeValue(bos, existingMap);
|
||||||
|
bos.flush(); // 强制刷新
|
||||||
|
}
|
||||||
|
|
||||||
|
// 可选占用测试
|
||||||
|
boolean released = file.renameTo(file);
|
||||||
|
System.out.println("文件释放状态: " + released);
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
return "学生答案已经写入到 " + filePath;
|
return "学生答案已经写入到 " + filePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static String convertToLetter(int number) {
|
public static String convertToLetter(int number) {
|
||||||
// 判断是否在 1 到 26 的范围内
|
// 判断是否在 1 到 26 的范围内
|
||||||
if (number >= 1 && number <= 26) {
|
if (number >= 1 && number <= 26) {
|
||||||
|
@@ -29,14 +29,18 @@ public class LogFileUtils {
|
|||||||
file.getParentFile().mkdirs(); // 创建父目录
|
file.getParentFile().mkdirs(); // 创建父目录
|
||||||
file.createNewFile();
|
file.createNewFile();
|
||||||
}
|
}
|
||||||
writer = new BufferedWriter(new FileWriter(file, true)); // 追加写入模式
|
|
||||||
|
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, true))) {
|
||||||
|
writer.write(""); // 可选写入内容
|
||||||
|
}
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error(e.getMessage());
|
throw new RuntimeException("无法创建日志文件: " + e.getMessage(), e);
|
||||||
throw new RuntimeException("无法创建日志文件: " + e.getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 写入文本,自动换行
|
* 写入文本,自动换行
|
||||||
* @param content 写入的文本
|
* @param content 写入的文本
|
||||||
|
Reference in New Issue
Block a user