public void saveDataToFile() { try { String courseFilePath = "data/courses.txt"; // 替换为保存 course 数据的文本文件路径 String numberFilePath = "data/credits.txt"; // 替换为保存 number 数据的文本文件路径 //String scoresFilePath = "data/scores/file.txt"; // 替换为保存 scores 数据的文本文件路径 String semesterFilePath = "data/semesters.txt"; // 替换为保存 semester 数据的文本文件路径 String typeFilePath = "data/types.txt"; // 替换为保存 type 数据的文本文件路径 saveListToFile(course, courseFilePath); saveListToFile(number, numberFilePath); Files.write(Paths.get("data/scores.txt"), scores.stream().map(String::valueOf).collect(Collectors.toList()), StandardCharsets.UTF_8); saveListToFile(semester, semesterFilePath); saveListToFile(type, typeFilePath); JOptionPane.showMessageDialog(null, "Data saved to files."); } catch (IOException e) { JOptionPane.showMessageDialog(null, "An error occurred while saving data to file: "); } }代码经常返回异常,请优化这个代码,使数据可以正确保存到txt文件中
时间: 2023-07-16 15:12:33 浏览: 66
可能的优化如下:
1. 检查文件路径是否存在,如果不存在则创建它们。
2. 使用 try-with-resources 语句来自动关闭文件流,防止资源泄漏。
3. 使用 BufferedWriter 来写入数据,这将提高写入大量数据时的效率。
4. 将异常信息打印到控制台上,以便更容易地调试问题。
下面是优化后的代码:
```
public void saveDataToFile() {
try {
String courseFilePath = "data/courses.txt";
String numberFilePath = "data/credits.txt";
String scoresFilePath = "data/scores.txt";
String semesterFilePath = "data/semesters.txt";
String typeFilePath = "data/types.txt";
// Check if directories exist, create them if not.
Files.createDirectories(Paths.get("data"));
// Write course data to file.
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(courseFilePath))) {
for (String course : course) {
writer.write(course);
writer.newLine();
}
}
// Write number data to file.
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(numberFilePath))) {
for (String number : number) {
writer.write(number);
writer.newLine();
}
}
// Write scores data to file.
Files.write(Paths.get(scoresFilePath), scores.stream().map(String::valueOf).collect(Collectors.toList()), StandardCharsets.UTF_8);
// Write semester data to file.
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(semesterFilePath))) {
for (String semester : semester) {
writer.write(semester);
writer.newLine();
}
}
// Write type data to file.
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(typeFilePath))) {
for (String type : type) {
writer.write(type);
writer.newLine();
}
}
JOptionPane.showMessageDialog(null, "Data saved to files.");
} catch (IOException e) {
System.err.println("An error occurred while saving data to file: " + e.getMessage());
JOptionPane.showMessageDialog(null, "An error occurred while saving data to file.");
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)