IO流对象序列化与反序列化
2016-06-30 00:03
387 查看
package com.mipo.file; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; /** * ObjectInputStream和ObjectOutputStream类是用于存储和读取基本数据类型或对象的过滤流, * 可以把Java中的对象写到数据源中,也可以把对象从数据源中还原回来 * 此二者不可序列化 static和transient修饰的成员变量,以及方法 * 可序列化 属性(包括基本数据类型、数组、对其它对象的引用),类名 * * 对象序列化:将对象存储到外存中或以二进制形式进行网络传输。 * 对象反序列化:从这些数据中重构一个与原始对象状态相同的对象。 * * 序列化的好处在于,它可以将任何实现了Serializable接口的对象转换为字节数据。 * 这些数据可以保存在数据源中,以后仍可以还原为原来的对象状态,即使这些数据通过网络传输到别处也能还原回来。 * Serializable是一个空接口,当一个类声明要实现Serializable时,只是表明该类可序列化。 * @author Administrator * */ public class TestSerializable { public static void main(String[] args) { test1(); test2(); } //**********序列化示例************************** public static void test1 () { ObjectOutputStream oos = null; try { //创建连接到指定文件的对象输出流实例 oos = new ObjectOutputStream(new FileOutputStream("D:\\Personal\\Desktop\\IO\\File\\demo\\test1.dat")); //把student对象序列化到文件中 oos.writeObject(new Student(101,"鸣人",22)); //刷新输出流 oos.flush(); System.out.println("序列化成功"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (null != oos) { try { oos.close();//关闭输出流实例 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } //*******************反序列化实例*************** public static void test2 () { ObjectInputStream ois = null; try { //创建连接到指定文件的输入流实例 ois = new ObjectInputStream(new FileInputStream("D:\\Personal\\Desktop\\IO\\File\\demo\\test1.dat")); //读取对象 Student stu = (Student) ois.readObject(); //输出读到的对象信息 System.out.println(stu); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (null != ois) { try { ois.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }//关闭对象输入流 } } } } //创建学生类**************************************** class Student implements java.io.Serializable { private int id; private String name; private transient int age;//不需要序列化的属性 public Student() {} public Student(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } 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; } //必须重写toString方法,输出是Student [id=101, name=鸣人, age=0],否则输出对象地址 @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树