定义包含学号、姓名和成绩信息的学生类Student。在测试类中定义存放n个学生对象存放到某个集合对象中(具体集合自行确定),然后按成绩进行降序排列,并将排序结果(类Student对象)保存到文件stud.dat中,再读出文件stud.dat中所有的学生信息并显示。Java
时间: 2024-03-03 14:50:31 浏览: 74
好的,这是一个比较完整的程序,需要分为三个部分:定义类、测试类和文件读写。我先给出完整代码,然后一步一步解释。
```java
import java.io.*;
import java.util.*;
class Student implements Serializable, Comparable<Student>{
int sno;
String name;
double score;
public Student(int sno, String name, double score){
this.sno = sno;
this.name = name;
this.score = score;
}
@Override
public int compareTo(Student o) {
return Double.compare(o.score, this.score);
}
@Override
public String toString() {
return "Student{" +
"sno=" + sno +
", name='" + name + '\'' +
", score=" + score +
'}';
}
}
public class Test {
public static void main(String[] args) {
int n = 5;
List<Student> students = new ArrayList<>();
for (int i = 0; i < n; i++) {
Student student = new Student(i, "Student" + i, Math.random() * 100);
students.add(student);
}
Collections.sort(students);
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("stud.dat"))) {
for (Student student : students) {
oos.writeObject(student);
}
} catch (IOException e) {
e.printStackTrace();
}
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("stud.dat"))) {
while (true) {
Student student = (Student) ois.readObject();
System.out.println(student);
}
} catch (EOFException e) {
// end of file
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
```
首先是定义类`Student`,需要实现`Serializable`和`Comparable`接口。`Serializable`接口是序列化接口,可以将对象转换为字节流,方便存储和传输。`Comparable`接口是比较接口,可以进行排序。
```java
class Student implements Serializable, Comparable<Student>{
int sno;
String name;
double score;
public Student(int sno, String name, double score){
this.sno = sno;
this.name = name;
this.score = score;
}
@Override
public int compareTo(Student o) {
return Double.compare(o.score, this.score);
}
@Override
public String toString() {
return "Student{" +
"sno=" + sno +
", name='" + name + '\'' +
", score=" + score +
'}';
}
}
```
接下来是测试类`Test`。首先生成`n`个随机学生,存放到`List`集合中。然后对集合进行排序,按照成绩从高到低排序。接着将排序结果写入文件`"stud.dat"`中,使用`ObjectOutputStream`对象将`Student`对象写入文件。最后读取文件中的数据,使用`ObjectInputStream`对象读取文件中的`Student`对象,直到文件结束。
```java
public class Test {
public static void main(String[] args) {
int n = 5;
List<Student> students = new ArrayList<>();
for (int i = 0; i < n; i++) {
Student student = new Student(i, "Student" + i, Math.random() * 100);
students.add(student);
}
Collections.sort(students);
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("stud.dat"))) {
for (Student student : students) {
oos.writeObject(student);
}
} catch (IOException e) {
e.printStackTrace();
}
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("stud.dat"))) {
while (true) {
Student student = (Student) ois.readObject();
System.out.println(student);
}
} catch (EOFException e) {
// end of file
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
```
需要注意的是,在使用`ObjectInputStream`对象读取文件时,需要在`catch`块中捕获`EOFException`异常,表示文件结束。同时需要捕获`IOException`和`ClassNotFoundException`异常,分别表示读取文件时出现的I/O错误和找不到`Student`类。
阅读全文