• Java的IO流-序列化流


    对象序列化:把Java对象写入到文件中去

    1. package com.itheima.d3;
    2. import java.io.FileOutputStream;
    3. import java.io.IOException;
    4. import java.io.ObjectOutputStream;
    5. public class Test1 {
    6. public static void main(String[] args) {
    7. try(
    8. //2、创建一个对象字节输出流包装原始的字节输出流
    9. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("IO/src/itheima11.txt"));
    10. ) {
    11. //1、创建一个Java对象
    12. User u = new User("rch", "任朝晖", 18, "rch041126");
    13. //3、序列化对象到文件中去
    14. oos.writeObject(u);
    15. System.out.println("序列化对象成功");
    16. } catch (Exception e) {
    17. e.printStackTrace();
    18. }
    19. }
    20. }

     注意:对象如果需要序列化,必须实现序列化接口

    1. package com.itheima.d3;
    2. import java.io.Serializable;
    3. //注意:对象如果需要序列化,必须实现序列化接口
    4. public class User implements Serializable {
    5. private String loginName;
    6. private String userName;
    7. private int age;
    8. //transient 这个成员变量不参与序列化
    9. private transient String passWord;
    10. public User() {
    11. }
    12. public User(String loginName, String userName, int age, String passWord) {
    13. this.loginName = loginName;
    14. this.userName = userName;
    15. this.age = age;
    16. this.passWord = passWord;
    17. }
    18. public String getLoginName() {
    19. return loginName;
    20. }
    21. public void setLoginName(String loginName) {
    22. this.loginName = loginName;
    23. }
    24. public String getUserName() {
    25. return userName;
    26. }
    27. public void setUserName(String userName) {
    28. this.userName = userName;
    29. }
    30. public int getAge() {
    31. return age;
    32. }
    33. public void setAge(int age) {
    34. this.age = age;
    35. }
    36. public String getPassWord() {
    37. return passWord;
    38. }
    39. public void setPassWord(String passWord) {
    40. this.passWord = passWord;
    41. }
    42. @Override
    43. public String toString() {
    44. return "User{" +
    45. "loginName='" + loginName + '\'' +
    46. ", userName='" + userName + '\'' +
    47. ", age=" + age +
    48. ", passWord='" + passWord + '\'' +
    49. '}';
    50. }
    51. }

    对象饭序列化:把文件里的Java对象读取出来

    1. package com.itheima.d3;
    2. import java.io.FileInputStream;
    3. import java.io.IOException;
    4. import java.io.ObjectInputStream;
    5. public class Test3 {
    6. public static void main(String[] args) {
    7. try(
    8. //1、创建一个对象字节输入流管道,包装 低级的字节输入流与源文件接通
    9. ObjectInputStream ois =
    10. new ObjectInputStream(new FileInputStream("IO/src/itheima11.txt"));
    11. ) {
    12. User u = (User) ois.readObject();
    13. System.out.println(u);
    14. } catch (IOException | ClassNotFoundException e) {
    15. e.printStackTrace();
    16. }
    17. }
    18. }

  • 相关阅读:
    Cy3-PEG-Folate,Cy3-聚乙二醇-叶酸,Folate/FA-PEG-Cy3
    软件工程毕业设计课题(55)微信小程序毕业设计JAVA企业公司小程序系统设计与实现
    【List-Watch】
    ps的一些快捷键
    容器基础--基本概念入门
    图像风格迁移的发展历程
    Doris学习笔记之介绍、编译安装与部署
    【学习笔记】[ARC156E] Non-Adjacent Matching
    Druid未授权访问--->ip:端口号/druid/index.html
    Python 列表推导式:简洁、高效的数据操作艺术
  • 原文地址:https://blog.csdn.net/rch041126/article/details/134517420