你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

ObjectInputStream读取文件对象ObjectOutputStream写入文件对象

2021/11/2 13:40:28

测试对象

public class User implements Serializable {

    Long serialVersionUID = 1L;


    private Long userId;

    private String userName;

    private String password;

    public User(Long userId, String userName, String password) {
        this.userId = userId;
        this.userName = userName;
        this.password = password;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

注意点:一定要进行实现序列化和反序列化 实现Serializable接口是将对象序列化,序列化的本质是将一个对象转成流。定义了serialVersionUID可以进行反序列化,值无所谓,但是一定要有这个属性

写入文件对象

public class ObjectOutputStreamDemo {
    public static void main(String[] args) {
        File file = new File("d:\\classTest.txt");
        ObjectOutputStream objectOutputStream = null;
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
            objectOutputStream = new ObjectOutputStream(outputStream);
            //objectOutputStream.writeUTF("www.baidu.com");
            objectOutputStream.writeObject(new User(1L,"zhong","123456"));
            objectOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                outputStream.close();
                objectOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

读取文件对象

public class ObjectInputStreamDemo {
    public static void main(String[] args) {
        File file = new File("d:\\classTest.txt");
        InputStream inputStream = null;
        ObjectInputStream objectInputStream = null;
        try {
            inputStream = new FileInputStream(file);
            objectInputStream = new ObjectInputStream(inputStream);
            //System.out.println(objectInputStream.readUTF());
            Object o = objectInputStream.readObject();
            if(o instanceof User){
                User user = (User) o;
                System.out.println(user);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
                objectInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}