java完成读取txt文件学生信息,并按成绩排序后输出
时间: 2023-08-04 13:27:53 浏览: 45
假设txt文件中每行存储的是一个学生的信息,包括姓名、年龄、性别、成绩,使用空格分隔。可以按照以下步骤读取txt文件,对学生信息进行排序,然后输出排序后的结果。
1. 创建一个Student类,包含学生的姓名、年龄、性别和成绩信息。
```java
public class Student implements Comparable<Student>{
private String name;
private int age;
private String gender;
private double score;
public Student(String name, int age, String gender, double score) {
this.name = name;
this.age = age;
this.gender = gender;
this.score = score;
}
// 省略getter和setter方法
@Override
public int compareTo(Student student) {
return Double.compare(this.score, student.getScore());
}
}
```
2. 读取txt文件中的学生信息并存储到List<Student>中。
```java
List<Student> students = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new FileReader("students.txt"));
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(" ");
String name = fields[0];
int age = Integer.parseInt(fields[1]);
String gender = fields[2];
double score = Double.parseDouble(fields[3]);
Student student = new Student(name, age, gender, score);
students.add(student);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
```
3. 对学生列表按照成绩进行排序。
```java
Collections.sort(students);
```
4. 输出排序后的结果。
```java
for (Student student : students) {
System.out.println(student.getName() + " " + student.getAge() + " " + student.getGender() + " " + student.getScore());
}
```
完整代码如下:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new FileReader("students.txt"));
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(" ");
String name = fields[0];
int age = Integer.parseInt(fields[1]);
String gender = fields[2];
double score = Double.parseDouble(fields[3]);
Student student = new Student(name, age, gender, score);
students.add(student);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
Collections.sort(students);
for (Student student : students) {
System.out.println(student.getName() + " " + student.getAge() + " " + student.getGender() + " " + student.getScore());
}
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)