您的位置:首页 > 编程语言 > Java开发

Java学习提要——对象序列化

2016-10-23 20:38 375 查看

1.对象序列化的基本应用

所谓的对象序列化指的就是将保存在内存中的对象数据

转换为二进制数据流进行传输的操作。

但是不是所有类的对象都可以进行序列化,

那么其所在的类一定要实现 java.io.Serializable接口

!其接口没有任何操作方法,它是一个标识接口,表示的是一种能力。

//定义一个可以被序列化对象的类
import java.io.Serializable;

@SuppressWarning("serial")
class Book implements Serializable {  //此类的对象可以被序列化
//压制警告信息,之所以有警告是由于序列化与反序列化的版本问题,可自行去了解
private String title ;
private double price ;
public Book(String title,double price) {
this.title = title ;
this.price = price ;
}
public String toString() {
return this.title + this.price;
}
}
public class Nice {
public static void main(String [] args) throws Exception{

}
}


实现序列化与反序列化,需要有两个类的支持

·序列化类:java.io.ObjectOutputStream;将对象变成了指定格式的二进制数据

·反序列化类:java.io.ObjectInputStream;可以将序列化的对象转换为对象内容

//实现序列化对象的操作——ObjectOutputStream

//构造方法
public ObjectOutputStream(OutputStream out) throws IOExeption;

//输出对象
public final void writeObject (Object obj) throws IOException;


//实现序列化
//将对象序列化到文件里面
import java.io.Serializable;

@SuppressWarning("serial")
class Book implements Serializable {  //此类的对象可以被序列化
//压制警告信息,之所以有警告是由于序列化与反序列化的版本问题,可自行去了解
private String title ;
private double price ;
public Book(String title,double price) {
this.title = title ;
this.price = price ;
}
public String toString() {
return this.title + "," + this.price;
}
}

public class Nice {
public static void main(String args []) throws Exception{
ser();
}
public static void ser() throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("E:"+File.separator+"test.ser"))
oos.writeObject(new Book("java",59.9));  //序列化对象
oos.close();
}
}


//实现反序列化的操作——ObjectInputStream

//构造方法
public ObjectInputStream(InputStream in) throws IOException;

//读取方法
public final Object readObject() throws IOException,ClassNotFoundException


//实现反序列化,接上面代码补充
//将对象序列化到文件里面,随后再通过文件反序列化到程序之中
import java.io.Serializable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

@SuppressWarning("serial")
class Book implements Serializable {  //此类的对象可以被序列化
//压制警告信息,之所以有警告是由于序列化与反序列化的版本问题,可自行去了解
private String title ;
private double price ;
public Book(String title,double price) {
this.title = title ;
this.price = price ;
}
public String toString() {
return this.title + "," + this.price;
}
}

public class Nice {
public static void main(String args []) throws Exception{
dser();
}
public static void ser() throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("E:"+File.separator+"test.ser"));
oos.writeObject(new Book("java",59.9));  //序列化对象
oos.close();
}
public static void dser() throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("E:"+File.separator+"test.ser")));
Object obj = ois.readObject();  //按照Object读取
Book book = (Book) obj;
System.out.println(book);
ois.close();
}
}


2.transient 关键字(了解)

序列化时,某些属性的内容不需要保存,就可以用此关键字

使用格式:private transient String str;

作用:这个属性将无法序列化。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: