Java 学习记录-18

本文展示了如何使用Java的ObjectOutputStream和ObjectInputStream进行对象的序列化和反序列化操作,以保存和加载Employee对象及ArrayList集合到文件中,包括单个对象和集合实例。
摘要由CSDN通过智能技术生成

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

import org.junit.Test;  
import java.io.*;  
public class ReadWriteDataOfAnyType {  
    @Test  
    public void save() throws IOException {  
        String name = "巫师";  
        int age = 300;  
        char gender = '男';  
        int energy = 5000;  
        double price = 75.5;  
        boolean relive = true;  
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("game.dat"));  
        oos.writeUTF(name);  
        oos.writeInt(age);  
        oos.writeChar(gender);  
        oos.writeInt(energy);  
        oos.writeDouble(price);  
        oos.writeBoolean(relive);  
        oos.close();  
    }  
    @Test  
    public void reload()throws IOException{  
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("game.dat"));  
        String name = ois.readUTF();  
        int age = ois.readInt();  
        char gender = ois.readChar();  
        int energy = ois.readInt();  
        double price = ois.readDouble();  
        boolean relive = ois.readBoolean();  
        System.out.println(name+"," + age + "," + gender + "," + energy + "," + price + "," + relive);  
        ois.close();  
    }  
}
import java.io.Serializable;  
public class Employee implements Serializable {  
    //static final long serialVersionUID = 23234234234L;  
    public static String company; //static 修饰的类变量,不会被序列化  
    public String name;  
    public String address;  
    public transient int age; // transient 瞬态修饰成员,不会被序列化  
    public Employee(String name, String address, int age) {  
        this.name = name;  
        this.address = address;  
        this.age = age;  
    }  
    public static String getCompany() {  
        return company;  
    }  
    public static void setCompany(String company) {  
        Employee.company = company;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public String getAddress() {  
        return address;  
    }  
    public void setAddress(String address) {  
        this.address = address;  
    }  
    public int getAge() {  
        return age;  
    }  
    public void setAge(int age) {  
        this.age = age;  
    }  
    @Override  
    public String toString() {  
        return "Employee{" +  
                "name='" + name + '\'' +  
                ", address='" + address + '\'' +  
                ", age=" + age +  
                ", company=" + company +  
                '}';  
    }  
}
----------------------------------------------------------------
import org.junit.Test;  
import java.io.*;  
public class ReadWriteObject {  
    @Test  
    public void save() throws IOException {  
        Employee.setCompany("尚硅谷");  
        Employee e = new Employee("小谷姐姐", "宏福苑", 23);  
        // 创建序列化流对象  
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("employee.dat"));  
        // 写出对象  
        oos.writeObject(e);  
        // 释放资源  
        oos.close();  
        System.out.println("Serialized data is saved"); // 姓名,地址被序列化,年龄没有被序列化。  
    }  
    @Test  
    public void reload() throws IOException, ClassNotFoundException {  
        // 创建反序列化流  
        FileInputStream fis = new FileInputStream("employee.dat");  
        ObjectInputStream ois = new ObjectInputStream(fis);  
        // 读取一个对象  
        Employee e = (Employee) ois.readObject();  
        // 释放资源  
        ois.close();  
        fis.close();  
        System.out.println(e);  
    }  
}
import java.io.Serializable;  
public class Employee implements Serializable {  
    //static final long serialVersionUID = 23234234234L;  
    public static String company; //static 修饰的类变量,不会被序列化  
    public String name;  
    public String address;  
    public transient int age; // transient 瞬态修饰成员,不会被序列化  
    public Employee(String name, String address, int age) {  
        this.name = name;  
        this.address = address;  
        this.age = age;  
    }  
    public static String getCompany() {  
        return company;  
    }  
    public static void setCompany(String company) {  
        Employee.company = company;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public String getAddress() {  
        return address;  
    }  
    public void setAddress(String address) {  
        this.address = address;  
    }  
    public int getAge() {  
        return age;  
    }  
    public void setAge(int age) {  
        this.age = age;  
    }  
    @Override  
    public String toString() {  
        return "Employee{" +  
                "name='" + name + '\'' +  
                ", address='" + address + '\'' +  
                ", age=" + age +  
                ", company=" + company +  
                '}';  
    }  
}
-------------------------------------------------------------------
import org.junit.Test;  
import java.io.*;  
import java.util.ArrayList;  
public class ReadWriteCollection {  
    @Test  
    public void save() throws IOException {  
        ArrayList<Employee> list = new ArrayList<>();  
        list.add(new Employee("张三", "宏福苑", 23));  
        list.add(new Employee("李四", "白庙", 24));  
        list.add(new Employee("王五", "平西府", 25));  
        // 创建序列化流对象  
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutpu  
                tStream("employees.dat"));  
        // 写出对象  
        oos.writeObject(list);  
        // 释放资源  
        oos.close();  
    }  
    @Test  
    public void reload() throws IOException, ClassNotFoundException {  
        // 创建反序列化流  
        FileInputStream fis = new FileInputStream("employees.dat");  
        ObjectInputStream ois = new ObjectInputStream(fis);  
        // 读取一个对象  
        ArrayList<Employee> list = (ArrayList<Employee>) ois.readObject();  
        // 释放资源  
        ois.close();  
        fis.close();  
        System.out.println(list);  
    }  
}

在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值