1编写一个程序运行Java控制台程序,检测本地是否保存学生对象(反序列化)如果保存,则输出学生信息如果没有保存,则通过学生类Student创建一个学生对象,将学生信息输出并保存到本地文件中(序列化)中

package homework2;

import java.io.Serializable;

public class Student implements Serializable{
	private static final long serialVersionUID = 1L;
	private String name;
	private char sex;
	
	
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(String name, char sex) {
		super();
		this.name = name;
		this.sex = sex;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public char getSex() {
		return sex;
	}
	public void setSex(char sex) {
		this.sex = sex;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

}





package homework2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Demo1 {
	public static void main(String[] args) {
		File file = new File("d://.txt");
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		Student stu = null;
 
		try {
			fis = new FileInputStream(file);
			ois = new ObjectInputStream(fis);
			stu = (Student) ois.readObject();
		} catch (FileNotFoundException e1) {
			System.out.println("文件不存在!");
			System.exit(1);
		} catch (IOException e) {
			System.out.println("反序列化失败:本地文件中不存在Student对象!");
		} catch (ClassNotFoundException e) {
			System.out.println("反序列化失败:本地文件中不存在Student对象!");
		}
 
		try {
			fos = new FileOutputStream(file);
			oos = new ObjectOutputStream(fos);
 
			if (stu instanceof Student) {
				System.out.println("反序列化成功!");
				System.out.println(stu);
			} else {
				System.out.println("开始写入新的学生对象!");
				stu = new Student("序列化", '男');
				oos.writeObject(stu);
				oos.flush();
				System.out.println("序列化成功:对象信息写入完成!");
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (fis != null) {
					fis.close();
				}
				if (ois != null) {
					ois.close();
				}
				if (fos != null) {
					fos.close();
				}
				if (oos != null) {
					oos.close();
				}
 
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}


	}





  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个示例代码实现: ```java import java.io.*; public class StudentTest { public static void main(String[] args) { String filePath = "D:\\student.txt"; Student student = null; try { FileInputStream fileIn = new FileInputStream(filePath); ObjectInputStream in = new ObjectInputStream(fileIn); student = (Student) in.readObject(); in.close(); fileIn.close(); System.out.println("从文件读取学生信息:"); System.out.println(student.toString()); } catch (IOException | ClassNotFoundException e) { System.out.println("未找到保存学生信息,创建新的学生对象保存。"); student = new Student("张三", 18, "篮球"); try { FileOutputStream fileOut = new FileOutputStream(filePath); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(student); out.close(); fileOut.close(); System.out.println("学生信息保存文件。"); } catch (IOException ex) { ex.printStackTrace(); } } } } class Student implements Serializable { private String name; private int age; private String hobby; public Student(String name, int age, String hobby) { this.name = name; this.age = age; this.hobby = hobby; } public String getName() { return name; } public int getAge() { return age; } public String getHobby() { return hobby; } @Override public String toString() { return "姓名:" + name + ",年龄:" + age + ",爱好:" + hobby; } } ``` 这个程序会首先尝试从指定文件读取学生对象,如果读取成功,就输出学生的信息;如果读取失败,就创建一个新的学生对象,并将其保存文件。在这个示例代码,我们使用了 Java序列化反序列化机制来实现学生对象保存和读取。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值