• JavaI/O系统2


    http://www.cnblogs.com/hellokitty1/p/4473952.html

    数据流:

    DataInputStream,DataOutputStream。可以用于与计算机无关的格式读写java的基本数据类型以及String对象

    对象流:

    ObjectInputStream,ObjectOutputStream.

    序列化:保存内存中对象的“全景图”。

    为了实现对象序列化,对应的类必须实现Serializable接口。

    Serializable接口中没有定义任何的方法,称之为“标记接口”。

    如果加上了一个transient修饰符,该属性的值将不被序列化。

    反序列化:使用ObjectInputStream类,将字节序列还原成对象的过程。

    复制代码
      1 package com.lovo.day2;
      2 
      3 import java.io.IOException;
      4 import java.io.Serializable;
      5 import java.util.Date;
      6 
      7 public class Student implements Serializable {
      8 
      9     private static final long serialVersionUID = 4282130869653374904L;
     10 
     11     private String name;
     12     private int age;
     13     private boolean sex;
     14     private double money;
     15     private Date birth;
     16     private Course course;
     17 
     18     public Student(String name, int age, boolean sex, double money, Date birth,
     19             Course course) {
     20         super();
     21         this.name = name;
     22         this.age = age;
     23         this.sex = sex;
     24         this.money = money;
     25         this.birth = birth;
     26         this.course = course;
     27     }
     28 
     29     public String getName() {
     30         return name;
     31     }
     32 
     33     public void setName(String name) {
     34         this.name = name;
     35     }
     36 
     37     public int getAge() {
     38         return age;
     39     }
     40 
     41     public void setAge(int age) {
     42         this.age = age;
     43     }
     44 
     45     public boolean isSex() {
     46         return sex;
     47     }
     48 
     49     public void setSex(boolean sex) {
     50         this.sex = sex;
     51     }
     52 
     53     public double getMoney() {
     54         return money;
     55     }
     56 
     57     public void setMoney(double money) {
     58         this.money = money;
     59     }
     60 
     61     public Date getBirth() {
     62         return birth;
     63     }
     64 
     65     public void setBirth(Date birth) {
     66         this.birth = birth;
     67     }
     68 
     69     public Course getCourse() {
     70         return course;
     71     }
     72 
     73     public void setCourse(Course course) {
     74         this.course = course;
     75     }
     76 
     77     @Override
     78     public String toString() {
     79         return "Student [name=" + name + ", age=" + age + ", sex=" + sex
     80                 + ", money=" + money + ", birth=" + birth + ", course="
     81                 + course + "]";
     82     }
     83 
     84  /*   private void readObject(java.io.ObjectInputStream in) throws IOException,
     85             ClassNotFoundException {
     86         System.out.println("读");
     87         this.name = in.readUTF();
     88         this.age = in.readInt();
     89         this.sex = in.readBoolean();
     90         this.course = (Course) in.readObject();
     91         this.money = in.readDouble();
     92     }
     93 
     94     private void writeObject(java.io.ObjectOutputStream out) throws IOException {
     95         System.out.println("写");
     96         out.writeUTF(this.name);
     97         out.writeInt(this.age);
     98         out.writeBoolean(this.sex);
     99         out.writeObject(course);
    100         out.writeDouble(this.money);
    101     }
    102   */
    103 }
    复制代码
    复制代码
     1 package com.lovo.day2;
     2 
     3 import java.io.Serializable;
     4 
     5 public class Course implements Serializable {
     6 
     7     private static final long serialVersionUID = 1L;
     8     
     9     private int code;
    10     private String name;
    11 
    12     public int getCode() {
    13         return code;
    14     }
    15 
    16     public void setCode(int code) {
    17         this.code = code;
    18     }
    19 
    20     public String getName() {
    21         return name;
    22     }
    23 
    24     public void setName(String name) {
    25         this.name = name;
    26     }
    27 
    28     public Course(int code, String name) {
    29         super();
    30         this.code = code;
    31         this.name = name;
    32     }
    33 
    34     @Override
    35     public String toString() {
    36         return "Course [code=" + code + ", name=" + name + "]";
    37     }
    38 }
    复制代码
     1 package com.lovo.day2;
     2 
     3 import java.io.FileInputStream;
     4 import java.io.FileNotFoundException;
     5 import java.io.FileOutputStream;
     6 import java.io.IOException;
     7 import java.io.ObjectInputStream;
     8 import java.io.ObjectOutputStream;
     9 import java.util.Date;
    10 
    11 public class ObjectTest {
    12 
    13     public static void main(String[] args) {
    14         Student stu = new Student("张飞", 18, true, 10.0, new Date(), new Course(
    15                 1, "语文"));
    16         Student stu2 = new Student("张飞2", 18, true, 10.0, new Date(), new Course(
    17                 2, "数学"));
    18 
    19         // 序列化
    20         ObjectOutputStream out = null;
    21 
    22         try {
    23             out = new ObjectOutputStream(
    24                     new FileOutputStream("d:\student.bin"));
    25             out.writeObject(stu);
    26             out.writeObject(stu2);
    27         } catch (FileNotFoundException e) {
    28             e.printStackTrace();
    29         } catch (IOException e) {
    30             e.printStackTrace();
    31         } finally {
    32             if (out != null) {
    33                 try {
    34                     out.close();
    35                 } catch (IOException e) {
    36                     e.printStackTrace();
    37                 }
    38             }
    39         }
    40 
    41         // 反序列化
    42         ObjectInputStream in = null;
    43 
    44         try {
    45             in = new ObjectInputStream(new FileInputStream("d:\student.bin"));
    46             Student stu1 = (Student) in.readObject();
    47             System.out.println(stu1);
    48 
    49             System.out.println("*****************");
    50 
    51             System.out.println(stu1.getCourse());
    52             
    53             System.out.println("#######################################");
    54             
    55             Student stu21 = (Student) in.readObject();
    56             System.out.println(stu21);
    57             
    58             System.out.println("*****************");
    59             
    60             System.out.println(stu21.getCourse());
    61         } catch (FileNotFoundException e) {
    62             e.printStackTrace();
    63         } catch (IOException e) {
    64             e.printStackTrace();
    65         } catch (ClassNotFoundException e) {
    66             e.printStackTrace();
    67         } finally {
    68             if (in != null) {
    69                 try {
    70                     in.close();
    71                 } catch (IOException e) {
    72                     e.printStackTrace();
    73                 }
    74             }
    75         }
    76     }
    77 }
  • 相关阅读:
    Oracle安装
    自动化测试开发
    Hyperf 接入阿里云ACM应用配置管理中心
    PHP redis有序集合实现分页
    虚拟现实与大数据
    树形数据结构化
    立即执行函数
    jQuery中的显示与隐藏
    jQuery中的HTML
    Django+Nginx配置+前后端交互
  • 原文地址:https://www.cnblogs.com/alloevil/p/5939603.html
Copyright © 2020-2023  润新知