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

Java--对象流、序列化和反序列化

2018-04-03 19:31 387 查看
一、对象流、序列化与反序列化
1. 使用对象流可以实现对象的序列化和反序列化操作
相关类
ObjectOutputStream (用于序列化)  输出
ObjectInputStream(用于反序列化)  输入
2.为什么要序列化: 易于保存,易于传输
3.序列化和反序列化的过程:
序列化是将对象的状态存储到特定存储介质中的过程(将对象转换成字节序列(二进制)的过程)
反序列化则是从特定存储介质中的数据重新构建对象的过程(将对象的二进制形式转换成对象)



4.序列化的步骤



5.反序列化步骤:





例题:序列化package serial;
import java.io.Serializable;
public class Person implements Serializable{
/**
* 添加一个控制序列化与反序列化版本一致性的serialVersionUID属性
*/
private static final long serialVersionUID = 1L;
private String name;
private int age;
private transient String sex; // 由transient修饰的属性不能被序列化保存
static String job; // 静态属性也不能被序列化保存
public Person() {
}
public Person(String name, int age,String sex) {
super();
this.name = name;
this.age = age;
this.sex=sex;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]"+job;
}
}
package serial;
import java.io.*;
public class SerialDemo {
public static void main(String[] args) {
File f=new File("c:"+File.separator+"person.txt");
Person per=new Person("胡夫",80,"男");
OutputStream out=null;
ObjectOutputStream oos=null;
try {
out=new FileOutputStream(f);
oos=new ObjectOutputStream(out);
Person.job="皇帝";
oos.writeObject(per);   // 序列化
System.out.println("序列化成功!");
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
运行结果:



例题:反序列化
package serial;
import java.io.*;
public class DeSerialDemo {
public static void main(String[] args) {
File f=new File("c:"+File.separator+"person.txt");
InputStream input=null;
ObjectInputStream ois=null;
try {
input=new FileInputStream(f);
ois=new ObjectInputStream(input);
Person per=(Person)ois.readObject(); // 反序列化
System.out.println(per);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}运行结果:


package serial;
import java.io.*;
import java.util.*;
public class ListSerial {
public static void main(String[] args) {
File f=new File("c:"+File.separator+"person.txt");
List<Person> list=new ArrayList<Person>();
Collections.addAll(list,new Person("张三",20,"男"),new Person("李四",25,"男"),new Person("王娟",26,"女"));
OutputStream out=null;
ObjectOutputStream oos=null;
try {
out=new FileOutputStream(f);
oos=new ObjectOutputStream(out);
oos.writeObject(list); // 序列化
System.out.println("序列化成功!");
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}运行结果:



package serial;
import java.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;

public class ListDeSerial {
public static void main(String[] args) {
File f=new File("c:"+File.separator+"person.txt");
InputStream input=null;
ObjectInputStream ois=null;
try {
input=new FileInputStream(f);
ois=new ObjectInputStream(input);
List<Person> list=(List<Person>)ois.readObject(); // 反序列化
for(Person per:list){
System.out.println(per);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  JavaSE