java对象序列化传输_Java 对象序列化与反序列化

对象序列化

对象序列化

对象序列化定义

所谓的对象序列化就是将 保存在内存中的对象数据转换为二进制数据流进行传输的操作 ;但不是所有对象都可以进行序列化,要被序列化的的对象那么其所在的类一定要实现 java.io.Serializable 接口,该接口并没有认识的操作方法,因为该接口是一个 标识接口 。

可以被序列化的类

import java.io.Serializable;

@SuppressWarnings("serial")

class Book implements Serializable {

private String title ;

private double price ;

public Book(String title , double price) {

this.price = price ;

this.title = title ;

}

public String toString() {

return "this.title + "\t" + this.price";

}

}

如上,就实现了一个可以被序列化的类,(使用了压制警告)。

如此,Book类就可以实现二进制的传输了!

实现序列化和反序列化

序列化类:

java.io.ObjectOutputStream

将对象转为指定格式的二进制数据

构造方法:

public ObjectOutputStream(OutputStream out)

输出对象:

public final void writeObject(Object obj)

反序列化类:

java.io.ObjectInputStream

将已经序列化的对象转换回原本的对象内容

构造方法:

public ObjectInputStream(InputStream in)

读取对象:

public final Object readObject()

实现序列化对象操作

@SuppressWarnings("serial")

class Book implements Serializable {

private String title ;

private double price ;

public Book(String title , double price) {

this.price = price ;

this.title = title ;

}

public String toString() {

return this.title + "\t" + this.price ;

}

}

public class TestDemo {

public static void main(String [] args) throws Exception {

ser();

}

public static void ser() throws Exception {

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("F:" + File.separator + "demo" + File.separator + "Byte.txt")));

oos.writeObject(new Book("Java开发",110.1));

oos.close();

}

}

实现反序列化类

public static void dser() throws Exception {

ObjectInputStream ois = new ObjectInputStream(new FileInputStream (new File("F:" + File.separator + "demo" + File.separator + "Byte.txt")));

Object obj = ois.readObject();// 按照Object读取

Book book = (Book) obj;// 向下转型

System.out.println(book);

ois.close();

}

序列化与反序列化完整实现

package helloworld;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.Serializable;

@SuppressWarnings("serial")

class Book implements Serializable {

private String title ;

private double price ;

public Book(String title , double price) {

this.price = price ;

this.title = title ;

}

public String toString() {

return this.title + "\t" + this.price ;

}

}

public class TestDemo {

public static void main(String [] args) throws Exception {

ser(); // 实现序列化

dser(); // 实现反序列化

}

public static void ser() throws Exception {

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("F:" + File.separator + "demo" + File.separator + "Byte.txt")));

oos.writeObject(new Book("Java开发",110.1));// 输出

oos.close();

}

public static void dser() throws Exception {

ObjectInputStream ois = new ObjectInputStream(new FileInputStream (new File("F:" + File.separator + "demo" + File.separator + "Byte.txt")));

Object obj = ois.readObject();// 按照Object读取

Book book = (Book) obj;// 向下转型

System.out.println(book);

ois.close();

}

}

transient 关键字

通过序列化和反序列化的code实现,我们发现:序列化操作时是将整个对象的所有属性内容进行保存;但是如果某些属性的内容不需要被保存就可以通过 transient 关键字定义。

private transient String title;

由定义可知,title属性不可以被序列化操作。

总结

不是所有的类都需要被序列化,只有需要传输的对象所在的类才需要序列化对象。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值