java中的对象流ObjectOutputStream和ObjectInputStream代码实例

package IO;

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

public class ObjectStreamDemo {
    public static void main(String[] args) {
        //writeObject();
        readObject();

    }

    /**
     * 反序列化过程
     * 从文件中把是对象内容读出来,还原为对象
     */
    private static void readObject(){
        File file = new File("c://test//dog.obj");
        InputStream out = null;
        try {
            out = new FileInputStream(file);
            ObjectInputStream oos = new ObjectInputStream(out);
            Dog dog=(Dog)oos.readObject();
            System.out.println(dog.toString());
            oos.close();
            System.out.println("成功执行");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    /**
     * 对象序列化
     * 把对象写入文件;市级写入的是类名,属性名,属性类型,属性值等
     */
    private static void writeObject(){
        Dog dog = new Dog("wang", 2, "母");
        File file = new File("c://test//dog.obj");
        OutputStream out = null;
        try {
            out = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(out);
            oos.writeObject(dog);
            oos.close();
            System.out.println("成功执行");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

}
package IO;

import java.io.Serializable;

/**
 * 如果一个对象需要实现序列化,那么该类必须实现Serializable接口
 * Serializable是一个标记接口,没有任何定义,为了告诉虚拟机JV该类对象可以被序列化
 * 什么时候需要序列化?
 * 1,把对象存储到文件中(存储到物理介质)
 * 2,对象在网络上传输
 * 如果对象没有实现Serializable接口,会报Java.io.NotSerilizableException错误
 */

public class Dog implements Serializable {
    private String name;
    private  int age;
    private  String sex;
    private transient int id;//在序列化中被忽略
    public Dog() {
    }

    public Dog(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是 `SerializationTest.java` 代码: ```java import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class SerializationTest { public static void main(String[] args) { Student student = new Student("张三", "男", 20); try (FileOutputStream fos = new FileOutputStream("E:\\IOTest\\objectSeri.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos)) { oos.writeObject(student); System.out.println("Student object has been serialized successfully"); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的代码,我们创建了一个 `Student` 对象,并将其序列化并写入到文件 `E:\IOTest\objectSeri.dat` 。 接下来是 `DeserializationTest.java` 代码: ```java import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; public class DeserializationTest { public static void main(String[] args) { try (FileInputStream fis = new FileInputStream("E:\\IOTest\\objectSeri.dat"); ObjectInputStream ois = new ObjectInputStream(fis)) { Student student = (Student) ois.readObject(); System.out.println("Student object has been deserialized successfully"); System.out.println("Name: " + student.getName()); System.out.println("Gender: " + student.getGender()); System.out.println("Age: " + student.getAge()); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } ``` 在上面的代码,我们从文件 `E:\IOTest\objectSeri.dat` 读取序列化的 `Student` 对象,并将其反序列化为一个新的 `Student` 对象。然后我们输出了这个对象的属性值。 需要注意的是,在 `DeserializationTest.java` ,我们需要将反序列化得到的 `Object` 强制转换成 `Student` 对象。 最后,运行 `SerializationTest.java` 和 `DeserializationTest.java` 两个程序,就可以实现将 `Student` 对象序列化到文件,并从文件反序列化得到这个对象了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值