对象序列化 Serializable
对象序列化就是把一个对象变为二进制的数据流的一种方法,通过对象序列化,可以方便的实现对象的传输和储存
如果一个对象想要实现序列化,就要实现Serializable接口,改接口如下
改接口没有任何方法,所有此接口是一个标识,标识这个类具备了序列化的能力。
package com.example.jpa.test; import java.io.Serializable; /** * com.xinguangnet.tuchao.merchant.manage * * @Author : Wukn * @Date : 2018/2/5 */ public class SerializableTest implements Serializable{ /** *name */ private String name; /** * in */ private Integer id; public SerializableTest(String name, Integer id) { this.name = name; this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } }
/** * 对象输出流 * * * 一个对象如果要进行输出,则必须使用ObjectOutputStream类 * ObjectOutputStream是OutputStream的子类 */ public void test01() throws Exception{ String s = "d:" + File.separator + "test.txt"; File file = new File( s ); //文件输出流 OutputStream out = new FileOutputStream( file ); //为对象输出流实例化 ObjectOutputStream oo = new ObjectOutputStream( out ); //保存对到文件 oo.writeObject( new SerializableTest("D",1)); oo.close(); } }
/** * 对象输入流 * * * 一个对象如果要进行输入,则必须使用ObjectintputStream类 * ObjectinputStream是inputStream的子类 */ public void test02() throws Exception{ String str = "d:" + File.separator + "ttest.txt"; File file = new File( str ); //文件输入流 InputStream in = new FileInputStream( file ); //为对象输入流实例化 ObjectInputStream oo = new ObjectInputStream( in ); Object o = oo.readObject(); oo.close(); //读取对象 System.out.println(o);
Externalizable接口
被Serierializable接口申明的类的对象的内容都将被序列化,如果现在用户指定序列化的内容,则可以让这个类实现Externalizable接口
此接口的构造方法如下:
Externalizable接口是Serierializable的子类,此接口有两个方法
【注意】在使用Externalizable接口序列化时,该类一定要有一个无参的构造方法,不然会报错。
package com.example.jpa.test; import java.io.*; /** * com.xinguangnet.tuchao.merchant.manage * * @Author : Wukn * @Date : 2018/2/5 */ public class SerializableTest implements Externalizable{ /** *name */ private String name; /** * in */ private Integer id; public SerializableTest() { } public SerializableTest(String name, Integer id) { this.name = name; this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } /** 重写此方法,根据需要保存需要的内容,序列化 * * @param out * @throws IOException */ @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeObject( this.name ); out.writeObject( id ); } /** *重写此方法,根据需要读取内容,反序列化 * @param in * @throws IOException * @throws ClassNotFoundException */ @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { this.name = (String) in.readObject(); this.id = (Integer) in.readObject(); } }
package com.example.jpa.test; import java.io.*; /** * com.xinguangnet.tuchao.merchant.manage * * @Author : Wukn * @Date : 2018/2/5 */ //对象序列化 public class tranientTest implements Serializable{ /** * //使用tranient表示这个属性不会被序列化 */ public transient String name; /** * id */ private Integer id; public tranientTest(String name, Integer id) { this.name = name; this.id = id; } @Override public String toString() { return "tranientTest{" + "name='" + name + '\'' + ", id=" + id + '}'; } } class Df{ public static void main(String[] args) throws Exception { } /** * 系列化 * @throws Exception */ public void test01() throws Exception { String str = "d:" + File.separator + "test.txt"; File file = new File( str ); //文件输出流 OutputStream ou = new FileOutputStream( file ); //对象输出流 ObjectOutputStream oo = new ObjectOutputStream( ou ); oo.write(new tranientTest("we",12)); oo.close(); } /** * 反序列化 * @throws Exception */ public void test02() throws Exception { String str = "d:" + File.separator + "test.txt"; File file = new File( str ); //文件输出流 InputStream ou = new FileInputStream( file ); //对象输出流 ObjectInputStream oo = new ObjectInputStream( ou ); Object o = oo.readObject(); oo.close(); System.out.println(o); } }
[email protected]:wukn2018/test.git 代码地址