JAVA实现创建一个学生对象,包括学号、姓名和班级信息,利用标准化输入设置3个学生信息,并实现三个学生对象的序列化和反序列化。
时间: 2024-05-07 19:23:43 浏览: 29
以下是一个JAVA实现创建学生对象、序列化和反序列化的示例代码:
```java
import java.io.*;
public class Student implements Serializable {
private String studentNumber;
private String name;
private String className;
public Student(String studentNumber, String name, String className) {
this.studentNumber = studentNumber;
this.name = name;
this.className = className;
}
public String getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public static void main(String[] args) {
Student[] students = new Student[3];
// 从标准输入读取3个学生信息
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
for (int i = 0; i < 3; i++) {
System.out.println("请输入第" + (i + 1) + "个学生信息:");
System.out.print("学号:");
String studentNumber = reader.readLine();
System.out.print("姓名:");
String name = reader.readLine();
System.out.print("班级:");
String className = reader.readLine();
students[i] = new Student(studentNumber, name, className);
}
} catch (IOException e) {
e.printStackTrace();
}
// 序列化学生对象
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("students.dat"));
for (int i = 0; i < 3; i++) {
oos.writeObject(students[i]);
}
oos.flush();
System.out.println("序列化学生对象成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (oos != null) {
oos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 反序列化学生对象
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("students.dat"));
for (int i = 0; i < 3; i++) {
Student student = (Student) ois.readObject();
System.out.println("学生信息:学号=" + student.getStudentNumber() + ", 姓名=" + student.getName() + ", 班级=" + student.getClassName());
}
System.out.println("反序列化学生对象成功!");
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (ois != null) {
ois.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
运行该程序,可以按照提示输入3个学生的信息,然后程序会将这些学生对象序列化到文件 `students.dat` 中,并且读取该文件,反序列化出之前序列化的学生对象,输出它们的信息。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)