JavaI/O系统2

简介: 数据流: DataInputStream,DataOutputStream。可以用于与计算机无关的格式读写java的基本数据类型以及String对象 对象流: ObjectInputStream,ObjectOutputStream. 序列化:保存内存中对象的“全景图”。

数据流:

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 }

 

 

相关文章
|
前端开发 Python
如何用Python快速搭建一个文件传输服务
如何用Python快速搭建一个文件传输服务
|
SQL HIVE
【Hive SQL 每日一题】环比增长率、环比增长率、复合增长率
该文介绍了环比增长率、同比增长率和复合增长率的概念及计算公式,并提供了SQL代码示例来计算商品的月度增长率。环比增长率是相邻两期数据的增长率,同比增长率是与去年同期相比的增长率,复合增长率则是连续时间段内平均增长的速率。文章还包含了一组销售数据用于演示如何运用这些增长率进行计算。
754 4
Latex更改字体颜色以及快速生成 SCI 论文的 revised version 和 pure version
Latex更改字体颜色以及快速生成 SCI 论文的 revised version 和 pure version
Latex更改字体颜色以及快速生成 SCI 论文的 revised version 和 pure version
|
9月前
|
前端开发 Linux 数据库
CMS网站管理系统如何选择CMS建站?
本文介绍了几款常用CMS系统,包括PageAdmin CMS,适用于公司、政务和学校等事业单位网站。该系统有上千款模板库,适合基础一般建站用户需求,同时支持Windows、Linux、麒麟服务器等多环境版本,支持国产化改造。
141 6
|
10月前
|
存储 监控 算法
Java内存管理的艺术:深入理解垃圾回收机制####
本文将引领读者探索Java虚拟机(JVM)中垃圾回收的奥秘,解析其背后的算法原理,通过实例揭示调优策略,旨在提升Java开发者对内存管理能力的认知,优化应用程序性能。 ####
146 0
|
11月前
|
Go 索引
go语音中range 循环
go语音中range 循环
140 12
|
存储 缓存 弹性计算
重新审视 CXL 时代下的分布式内存
从以太网到 RDMA 再到 CXL,标志着互连技术的重大突破。
|
NoSQL Java API
分布式锁【数据库乐观锁实现的分布式锁、Zookeeper分布式锁原理、Redis实现的分布式锁】(三)-全面详解(学习总结---从入门到深化)(上)
分布式锁【数据库乐观锁实现的分布式锁、Zookeeper分布式锁原理、Redis实现的分布式锁】(三)-全面详解(学习总结---从入门到深化)
317 0
N..
|
前端开发 开发者
CSS高级应用
CSS高级应用
N..
108 0