JavaI/O系统2
数据流:
DataInputStream,DataOutputStream。可以用于与计算机无关的格式读写java的基本数据类型以及String对象
对象流:
ObjectInputStream,ObjectOutputStream.
序列化:保存内存中对象的“全景图”。
为了实现对象序列化,对应的类必须实现Serializable接口。
Serializable接口中没有定义任何的方法,称之为“标记接口”。
如果加上了一个transient修饰符,该属性的值将不被序列化。
反序列化:使用ObjectInputStream类,将字节序列还原成对象的过程。
package com.lovo.day2; import java.io.IOException;
import java.io.Serializable;
import java.util.Date; public class Student implements Serializable { private static final long serialVersionUID = 4282130869653374904L; private String name;
private int age;
private boolean sex;
private double money;
private Date birth;
private Course course; public Student(String name, int age, boolean sex, double money, Date birth,
Course course) {
super();
this.name = name;
this.age = age;
this.sex = sex;
this.money = money;
this.birth = birth;
this.course = course;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public boolean isSex() {
return sex;
} public void setSex(boolean sex) {
this.sex = sex;
} public double getMoney() {
return money;
} public void setMoney(double money) {
this.money = money;
} public Date getBirth() {
return birth;
} public void setBirth(Date birth) {
this.birth = birth;
} public Course getCourse() {
return course;
} public void setCourse(Course course) {
this.course = course;
} @Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", sex=" + sex
+ ", money=" + money + ", birth=" + birth + ", course="
+ course + "]";
} /* private void readObject(java.io.ObjectInputStream in) throws IOException,
ClassNotFoundException {
System.out.println("读");
this.name = in.readUTF();
this.age = in.readInt();
this.sex = in.readBoolean();
this.course = (Course) in.readObject();
this.money = in.readDouble();
} private void writeObject(java.io.ObjectOutputStream out) throws IOException {
System.out.println("写");
out.writeUTF(this.name);
out.writeInt(this.age);
out.writeBoolean(this.sex);
out.writeObject(course);
out.writeDouble(this.money);
}
*/
}
package com.lovo.day2;
import java.io.Serializable;
public class Course implements Serializable {
private static final long serialVersionUID = 1L;
private int code;
private String name;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Course(int code, String name) {
super();
this.code = code;
this.name = name;
}
@Override
public String toString() {
return "Course [code=" + code + ", name=" + name + "]";
}
}
package com.lovo.day2; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date; public class ObjectTest { public static void main(String[] args) {
Student stu = new Student("张飞", 18, true, 10.0, new Date(), new Course(
1, "语文"));
Student stu2 = new Student("张飞2", 18, true, 10.0, new Date(), new Course(
2, "数学")); // 序列化
ObjectOutputStream out = null; try {
out = new ObjectOutputStream(
new FileOutputStream("d:\\student.bin"));
out.writeObject(stu);
out.writeObject(stu2);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} // 反序列化
ObjectInputStream in = null; try {
in = new ObjectInputStream(new FileInputStream("d:\\student.bin"));
Student stu1 = (Student) in.readObject();
System.out.println(stu1); System.out.println("*****************"); System.out.println(stu1.getCourse()); System.out.println("#######################################"); Student stu21 = (Student) in.readObject();
System.out.println(stu21); System.out.println("*****************"); System.out.println(stu21.getCourse());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
JavaI/O系统2的更多相关文章
- JavaI/O系统
I/O:(输入/输出)指的是计算机与外部世界,或者一个与计算机其余部分的接口.它对任何计算机系统都非常关键. Java类库中有大量的类,帮助我们从不同的设备读取数据并保存或输出到不同的设备中. 这些类 ...
- JavaI/O 系统
1.JavaI/O 系统概述 A. 输入输出(I/O):指的是计算机与外部世界,或者一个程序与计算机的其余部分之间的接口 B. 流的概念(流:Stream) 流的基本特性:有数据.有方向 2. 流的 ...
- Lucene的入门
Lucene准备 Lucene可以在官网上下载,我这里已经下载好了,用的是4.10.3版本的, 解压以后的文件为: 如果没有使用maven管理的话,就需要引入这三个jar包,实现lucene功能. 我 ...
- JAVA编程不得不看的几本经典书籍
为了帮助对java编程感兴趣的同学更好.更快的提高编程技术,武汉北大青鸟光谷校区专业老师在此推荐几本学习编程非常有用的书籍,以供大家参考. 入门类 1.<java从入门到精通>(第3版) ...
- 学习Java的书籍资料
对于程序员来说,编程技术至关重要,然而技术的提高不是一蹴而就的,它需要时间的积累和经验的沉淀.因此本文为大家推荐Java学习的书籍,学虽容易,学好不易,且学且珍惜. 基础类.<Java从入门到精 ...
- 2012高校GIS论坛
江苏省会议中心 南京·钟山宾馆(2012年4月21-22日) 以"突破与提升"为主题的"2012高校GIS论坛"将于4月在南京举行,由南京大学和工程中心共同承办 ...
- 在Openfire上弄一个简单的推送系统
推送系统 说是推送系统有点大,其实就是一个消息广播功能吧.作用其实也就是由服务端接收到消息然后推送到订阅的客户端. 思路 对于推送最关键的是服务端向客户端发送数据,客户端向服务端订阅自己想要的消息.这 ...
- 数据库优化案例——————某市中心医院HIS系统
记得在自己学习数据库知识的时候特别喜欢看案例,因为优化的手段是容易掌握的,但是整体的优化思想是很难学会的.这也是为什么自己特别喜欢看案例,今天也开始分享自己做的优化案例. 最近一直很忙,博客产出也少的 ...
- ABP文档 - 通知系统
文档目录 本节内容: 简介 发送模式 通知类型 通知数据 通知重要性 关于通知持久化 订阅通知 发布通知 用户通知管理器 实时通知 客户端 通知存储 通知定义 简介 通知用来告知用户系统里特定的事件发 ...
随机推荐
- Java web 调试技巧之查看浏览器中调试中的network
在java web开发过程中尤其后台开发经常需要查看浏览器调试中的network项:今天在开发在线预览系统时用到了flexpaper插件,这个插件会调用FlexPaperViewer.swf这个软件( ...
- Latex中Matlab代码的环境
需要用到listings宏包 使用方法: 导言区\usepackage{listings}\lstset{language=Matlab} %代码语言使用的是matlab\lstset{br ...
- Python学习-24.Python中的算术运算
加法:+,与C#中并无区别,并且一样可以作用于字符串. 但Python中不支持字符串与数值类型的相加. i = 1 s = ' print(s + i) 这样是会在运行时报错的,正确写法如下: i = ...
- DI spring.net简单使用
IOC或DI spring.net简单使用 一.spring.net是什么? Spring 框架本是 Java 平台上一个应用非常多的.开源的框架.虽然语言是固定的,但是好的方法应该是通用的,于是 ...
- LinkedBlockingQueue源码解析(3)
此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 4.3.public E take() throws InterruptedException 原理: 将队 ...
- POI2014 RAJ-Rally
Description 给定一个\(N\)个点\(M\)条边的\(DAG(N,M\leq10^6)\),边权为\(1\).删去一个点,使剩余图中的最长路径最短,求删去的点和最长路径长度. Soluti ...
- react.js学习之路二
看react.js对我来说真的不是难不难的问题,问题是我的思路太难转变了,真是坑死我了,react里面的坑也很多.算了,这些抱怨没啥用,记录一下今天学习的内容. 今天看了to-do-list经典示例 ...
- 双11,自动领取优惠券 js 代码
http://www.zhihu.com/question/36426051/answer/67690322 本来我不熟悉 js,正好研究一下 (function(window, document) ...
- 基于Zookeeper实现的分布式互斥锁 - InterProcessMutex
Curator是ZooKeeper的一个客户端框架,其中封装了分布式互斥锁的实现,最为常用的是InterProcessMutex,本文将对其进行代码剖析 简介 InterProcessMutex基于Z ...
- [As3.0] 获取本机信息
package { import flash.display.Sprite; import flash.events.Event; import flash.net.NetworkInfo; impo ...