JAVA 对象输入流和对象输出流在java中的应用

对象输出流的概述:

import java.io.Serializable;

public class Student implements Serializable {
    private static final long seriaVersionUID = 1; //把序列号给固定下来 否则以后改了类就会出现问题
    public  String name;
    public int age;
    public Student(String name,int age){
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
import java.io.*;

/*
* 对象操作流:可以用于读写任意类型的对象 可以和类相结合 你创建完类后 别忘了一句 public class Student implements Serializable 来实现接口
*  注意一点: Serializable :序列号 是一个标识接口 只是起标识作用 没有方法
*          当一个类的对象需要IO接口进行读写的时候,这个类必须实现该接口
*         ObjectOutputStream
*         方法writeObject用于将一个对象写入流中。
*         ObjectOutputStream(OutputStream out)
创建一个写入指定的OutputStream的ObjectOutputStream。
*         ObjectInputStream
*         readObject 读对象
*O         bjectInputStream(InputStream in)。
*         注意:使用对象输出流来输出对象 只能使用对象输入流来输入对象
*         只有支持java.io.Serializable接口的对象才能写入流中。
*
*         Exception in thread "main" java.io.NotSerializableException: Student
*         抛出一个实例需要一个Serializable接口。 序列化运行时或实例的类可能会抛出此异常。
*         Exception in thread "main" java.io.EOFException :读到文件或者流的末尾抛出此异常
*
*         */
public class ObjectOutputStreamDemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //创建对象输出流的对象
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
        //创建学生对象
        Student s = new Student("korol",17);
        Student s2 = new Student("meiko",17);
        //写出学生对象
        oos.writeObject(s);
        oos.writeObject(s2);  //注意一点啊 你写进去的东西你是没有办法看的 都是乱码 你要看的话需要用对象输入流去看
        //释放资源
        oos.close();


        //创建对象输入流的对象
        ObjectInputStream ois = new  ObjectInputStream(new FileInputStream("a.txt"));
        //读取对象
//        Object obj = ois.readObject();
//        System.out.println(obj);
//        Object obj2 = ois.readObject();
//        System.out.println(obj2);
//        Object obj3 = ois.readObject();
//        System.out.println(obj3);
        try {
            while(true)
            {
                Object obj = ois.readObject();
                Student s3= (Student)obj; //这里使用向下转型 **********************************      很重要        ****************
            System.out.println(s3.getName());  //这里改写toString方法
            }
        }catch(EOFException e){
            System.out.println("读到了文件末尾");
        }
        //释放资源
        ois.close();
    }
}

对象输出流和对象输入流的集合:

import java.io.*;
import java.util.ArrayList;

/*
* 解决对象输入流读取对象出现异常的问题
* */
public class ObjectOutputStreamDemo2 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //创建对象输出流的对象
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("b.txt"));
        //创建集合对象
        ArrayList<Student> list = new ArrayList<Student>();
        //添加学生对象
        list.add(new Student("wangwu",30));
        list.add(new Student("lisan",17));
        //写出集合对象
        oos.writeObject(list);
        //释放资源
        oos.close();

        //创建对象输入流的对象
        ObjectInputStream ois = new  ObjectInputStream(new FileInputStream("b.txt"));
        //读取数据
        Object obj = ois.readObject();
        //System.out.println(obj);
        //向下转型,获取具体的子类对象
        ArrayList<Student> list2 = (ArrayList<Student>)obj;
        for(Student s : list)
            System.out.println(s);
        //释放资源
        ois.close();
    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值