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("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"); }请使用Bufferedreader来读取数据,帮我优化这个代码
时间: 2023-06-27 16:08:30 浏览: 93
好的,这里是使用BufferedReader来读取数据的优化代码:
```
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 (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("data/" + file), StandardCharsets.UTF_8))) {
List<String> lines = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
fileContents.add(lines);
} 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");
}
```
使用BufferedReader来读取数据可以提高读取文件的效率,同时也可以避免文件编码不匹配的问题。
阅读全文