public void importData() { try { List<String> courses = Files.readAllLines(Paths.get("data/courses.txt"), StandardCharsets.UTF_8); List<String> scores = Files.readAllLines(Paths.get("data/scores.txt"), StandardCharsets.UTF_8); List<String> ids = Files.readAllLines(Paths.get("data/numbers.txt"), StandardCharsets.UTF_8); List<String> semesters = Files.readAllLines(Paths.get("data/semesters.txt"), StandardCharsets.UTF_8); List<String> courseTypes = Files.readAllLines(Paths.get("data/types.txt"), StandardCharsets.UTF_8); students.clear(); for (int i = 0; i < courses.size(); i++) { String course = courses.get(i); String id = ids.get(i); String score = scores.get(i); String semester = semesters.get(i); String courseType = courseTypes.get(i); addData(course,id,score,semester,courseType); Student student = new Student(course, score,id, semester, courseType); students.add(student); //updateDisplay(); } //updateDisplay(); JOptionPane.showMessageDialog(null, "Data has been imported"); } catch (IOException e) { JOptionPane.showMessageDialog(null, "An error occurred importing data"); } }
时间: 2023-07-15 16:12:37 浏览: 32
这是一个Java函数,用于从文件中导入学生的课程、成绩、学号、学期和课程类型信息,并将它们添加到一个名为“students”的列表中。函数使用Java的文件I/O库来读取文件中的行。其中,“courses.txt”文件包含课程名称, “scores.txt”文件包含成绩, “numbers.txt”文件包含学生学号, “semesters.txt”文件包含学期信息,而“types.txt”文件包含课程类型。
在函数的主体中,使用一个for循环来遍历每个文件中的行。然后,函数解析每行数据并将其传递给“addData”函数,以便将其添加到“students”列表中。此外,函数还创建了一个“Student”对象,该对象包含有关学生的课程、成绩、学号、学期和课程类型的信息,并将其添加到“students”列表中。最后,函数使用Java的JOptionPane类来显示一个消息框,指示数据是否成功导入。
请注意,此函数可能需要进行一些修改,具体取决于文件的格式和数据结构。
相关问题
public void importData() { try { List<String> courses = Files.readAllLines(Paths.get("data/courses.txt"), StandardCharsets.UTF_8); List<String> scores = Files.readAllLines(Paths.get("data/scores.txt"), StandardCharsets.UTF_8); List<String> ids = Files.readAllLines(Paths.get("data/numbers.txt"), StandardCharsets.UTF_8); List<String> semesters = Files.readAllLines(Paths.get("data/semesters.txt"), StandardCharsets.UTF_8); List<String> courseTypes = Files.readAllLines(Paths.get("data/types.txt"), StandardCharsets.UTF_8); students.clear(); for (int i = 0; i < courses.size(); i++) { String course = courses.get(i); String id = ids.get(i); String score = scores.get(i); String semester = semesters.get(i); String courseType = courseTypes.get(i); addData(course,id,score,semester,courseType); Student student = new Student(course, score,id, semester, courseType); students.add(student); //updateDisplay(); } //updateDisplay(); JOptionPane.showMessageDialog(null, "Data has been imported"); } catch (IOException e) { JOptionPane.showMessageDialog(null, "An error occurred importing data"); } }请帮我修改上述代码
在修改之前,需要了解一下这段代码的作用。这段代码是用于从文件中读取学生的课程、分数、学号、学期和课程类型等信息,并将这些信息添加到程序中,以便后续对学生数据进行处理和展示。
下面是对这段代码的修改建议:
1. 将方法的返回类型从 void 改为 boolean,以便在方法执行过程中出现错误时能够及时反馈给调用方。
2. 将读取文件的代码块包装在 try-catch 块中,以便处理文件读取中可能出现的异常。
3. 在读取文件之前,检查文件是否存在或者是否能够被读取,如果文件不存在或者无法被读取,则返回 false。
4. 在添加学生数据时,需要检查参数的合法性,如果参数为空或者格式不正确,则返回 false。
5. 添加学生数据时,需要判断该学生是否已经存在,如果已经存在,则更新该学生的数据,否则添加新的学生数据。
6. 在方法执行结束后,需要返回 true,表示数据已经成功导入。
下面是修改后的代码:
```
public boolean importData() {
try {
List<String> courses = Files.readAllLines(Paths.get("data/courses.txt"), StandardCharsets.UTF_8);
List<String> scores = Files.readAllLines(Paths.get("data/scores.txt"), StandardCharsets.UTF_8);
List<String> ids = Files.readAllLines(Paths.get("data/numbers.txt"), StandardCharsets.UTF_8);
List<String> semesters = Files.readAllLines(Paths.get("data/semesters.txt"), StandardCharsets.UTF_8);
List<String> courseTypes = Files.readAllLines(Paths.get("data/types.txt"), StandardCharsets.UTF_8);
if (courses.isEmpty() || scores.isEmpty() || ids.isEmpty() || semesters.isEmpty() || courseTypes.isEmpty()) {
return false; // 文件不存在或者无法被读取
}
for (int i = 0; i < courses.size(); i++) {
String course = courses.get(i);
String id = ids.get(i);
String score = scores.get(i);
String semester = semesters.get(i);
String courseType = courseTypes.get(i);
if (course.isEmpty() || id.isEmpty() || score.isEmpty() || semester.isEmpty() || courseType.isEmpty()) {
return false; // 参数为空或格式不正确
}
boolean studentExists = false;
for (Student student : students) {
if (student.getId().equals(id)) {
student.setCourse(course);
student.setScore(score);
student.setSemester(semester);
student.setCourseType(courseType);
studentExists = true;
break;
}
}
if (!studentExists) {
Student student = new Student(course, score, id, semester, courseType);
students.add(student);
}
}
JOptionPane.showMessageDialog(null, "Data has been imported");
return true;
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "An error occurred importing data");
return false;
}
}
```
需要注意的是,在修改代码时,可能需要根据实际情况对一些变量和方法进行调整。
public void importData() { List<String> files = Arrays.asList("courses.txt", "scores.txt", "credits.txt", "semesters.txt", "types.txt"); List<List<String>> fileContents = new ArrayList<>(); for (String file : files) { try (Stream<String> stream = Files.lines(Paths.get("F:\\ProgramData\\IdeaProjects\\BUPT-EBU6304G49-main\\SE0514\\src\\data\\" + file), StandardCharsets.UTF_8)) { fileContents.add(stream.collect(Collectors.toList())); } catch (IOException e) { JOptionPane.showMessageDialog(null, "An error occurred importing data"); return; } } this.courses.clear(); for (int i = 0; i < fileContents.get(0).size(); i++) { String course = fileContents.get(0).get(i); String score = fileContents.get(1).get(i); String credit = fileContents.get(2).get(i); String semester = fileContents.get(3).get(i); String courseType = fileContents.get(4).get(i); addData(course, credit, score, semester, courseType); this.courses.add(new Course(course, score, credit, semester, courseType)); } JOptionPane.showMessageDialog(null, "Data has been imported"); }代码经常返回异常,请优化这个代码,使数据可以正确从txt文件中读取
这段代码有几个问题需要优化:
1. 如果其中一个文件读取失败,整个方法就会返回并且数据无法正确导入,应该在每个文件读取失败后继续读取其他文件。
2. 在读取文件时,最好使用绝对路径,因为相对路径可能会因为不同的执行环境而不同。
3. 在解析文件内容时,最好检查文件内容的行数是否相等,否则会导致数据不匹配。
以下是优化后的代码:
```
public void importData() {
List<String> files = Arrays.asList("courses.txt", "scores.txt", "credits.txt", "semesters.txt", "types.txt");
List<List<String>> fileContents = new ArrayList<>();
for (String file : files) {
try (Stream<String> stream = Files.lines(Paths.get("F:/ProgramData/IdeaProjects/BUPT-EBU6304G49-main/SE0514/src/data/" + file), StandardCharsets.UTF_8)) {
fileContents.add(stream.collect(Collectors.toList()));
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Failed to import data from file: " + file);
}
}
if (fileContents.size() != 5) {
JOptionPane.showMessageDialog(null, "Failed to import data");
return;
}
// Check if each file has the same number of lines
int numLines = fileContents.get(0).size();
for (List<String> content : fileContents) {
if (content.size() != numLines) {
JOptionPane.showMessageDialog(null, "Failed to import data: files do not have the same number of lines");
return;
}
}
// Clear current data
this.courses.clear();
// Read data from files and add to list
for (int i = 0; i < numLines; i++) {
String course = fileContents.get(0).get(i);
String score = fileContents.get(1).get(i);
String credit = fileContents.get(2).get(i);
String semester = fileContents.get(3).get(i);
String courseType = fileContents.get(4).get(i);
addData(course, credit, score, semester, courseType);
this.courses.add(new Course(course, score, credit, semester, courseType));
}
JOptionPane.showMessageDialog(null, "Data has been imported");
}
```
相关推荐
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)