帮我优化以下代码import java.io.*; import java.time.LocalDate; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class Demo5 { public static void main(String[] args) throws IOException { ArrayList<Student> arr = new ArrayList<>(); File f1 = new File("input.txt"); FileInputStream fin = new FileInputStream(f1); BufferedReader br = new BufferedReader(new InputStreamReader(fin)); String msg = br.readLine(); while (msg != null) { if (msg.equals("")) { msg = br.readLine(); continue; } String[] stu = msg.split(" "); String num = stu[0]; String na = stu[1]; String da = stu[2]; String gen = stu[3]; Student student = new Student(Integer.parseInt(num), na,LocalDate.parse(da), gen); arr.add(student); Collections.sort(arr,Comparator.comparing(Student::getDate)); msg = br.readLine(); } br.close(); fin.close(); FileWriter writer = new FileWriter("output.txt"); for (Student obj:arr ) { writer.write(String.valueOf(obj)); writer.write("\r\n"); } writer.close(); } static class Student { private Integer number; private String name; private LocalDate date; private String gender; public LocalDate getDate() { return date; } public Student(Integer number, String name, LocalDate date, String gender) { this.number = number; this.name = name; this.date = date; this.gender = gender; } @Override public String toString() { return number + " " + name + " " + date + " " + gender; } } }
时间: 2024-01-18 08:04:20 浏览: 87
个人金融管理应用案例Java代码、建议及注意项.docx
这段代码主要有两个问题:
1. 没有使用 try-with-resources 语句处理文件流
2. 每次读取一行数据时,都需要进行字符串的操作,可能会影响性能
下面是我对代码进行的优化:
```java
import java.io.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Demo5 {
public static void main(String[] args) {
ArrayList<Student> arr = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("input.txt"));
FileWriter writer = new FileWriter("output.txt")) {
String msg;
while ((msg = br.readLine()) != null) {
if (msg.trim().isEmpty()) {
continue;
}
String[] stu = msg.split(" ");
int num = Integer.parseInt(stu[0]);
String na = stu[1];
LocalDate da = LocalDate.parse(stu[2]);
String gen = stu[3];
Student student = new Student(num, na, da, gen);
arr.add(student);
Collections.sort(arr, Comparator.comparing(Student::getDate));
}
for (Student obj : arr) {
writer.write(String.valueOf(obj));
writer.write(System.lineSeparator());
}
} catch (IOException e) {
e.printStackTrace();
}
}
static class Student {
private Integer number;
private String name;
private LocalDate date;
private String gender;
public LocalDate getDate() {
return date;
}
public Student(Integer number, String name, LocalDate date, String gender) {
this.number = number;
this.name = name;
this.date = date;
this.gender = gender;
}
@Override
public String toString() {
return number + " " + name + " " + date + " " + gender;
}
}
}
```
优化后的代码使用 try-with-resources 语句处理文件流,避免了手动关闭流的问题。同时,将读取文件和写入文件的逻辑放在了同一个 try-with-resources 语句块中,避免了代码中的重复。读取文件时,使用了 `trim()` 方法去掉字符串两端的空格,并且使用 `isEmpty()` 方法判断是否为空,避免了每次读取数据时进行字符串判断的问题。写入文件时,使用了 `System.lineSeparator()` 方法代替了 `"\r\n"`,这样可以在不同的操作系统上保持一致的换行符。
阅读全文