Java--对象流、序列化和反序列化
2018-04-03 19:31
387 查看
一、对象流、序列化与反序列化
1. 使用对象流可以实现对象的序列化和反序列化操作
相关类
ObjectOutputStream (用于序列化) 输出
ObjectInputStream(用于反序列化) 输入
2.为什么要序列化: 易于保存,易于传输
3.序列化和反序列化的过程:
序列化是将对象的状态存储到特定存储介质中的过程(将对象转换成字节序列(二进制)的过程)
反序列化则是从特定存储介质中的数据重新构建对象的过程(将对象的二进制形式转换成对象)
![](https://img-blog.csdn.net/20180403192436315?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0pvbmVzMDUyNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
4.序列化的步骤
![](https://img-blog.csdn.net/20180403192502358?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0pvbmVzMDUyNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
5.反序列化步骤:
![](https://img-blog.csdn.net/20180403192520696?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0pvbmVzMDUyNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
![](https://img-blog.csdn.net/20180403192544177?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0pvbmVzMDUyNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
例题:序列化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;
}
}
![](https://img-blog.csdn.net/20180403192713671?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0pvbmVzMDUyNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
例题:反序列化
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();
}
}
}
}运行结果:
![](https://img-blog.csdn.net/20180403192746946?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0pvbmVzMDUyNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
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();
}
}
}
}运行结果:
![](https://img-blog.csdn.net/20180403192938813?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0pvbmVzMDUyNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
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();
}
}
}
}运行结果:
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();
}
}
}
}运行结果:
相关文章推荐
- 初尝Java序列化/反序列化对象
- 【Java高级应用】对象序列化和反序列化
- java 对象 序列化 反序列化
- java对象序列化和反序列化
- java对象序列化和反序列化必须要注意
- java执行序列化和反序列化操作实现对象的clone
- Java对象序列化成字符串和反序列化
- Java对象的序列化和反序列化实践
- Java对象的序列化和反序列化
- Java知识(对象的序列化和反序列化)
- Java学习笔记之IO(十三):对象的序列化和反序列化
- java对象的序列化和反序列化
- Java基础学习总结——Java对象的序列化和反序列化
- Java IO流对象的序列化和反序列化实例详解
- Java基础学习总结——Java对象的序列化和反序列化
- 【Java学习笔记】对象的序列化和反序列化
- Java对象的序列化和反序列化
- Java 对象序列化和反序列化
- java对象与字符串之间的序列化和反序列化
- java序列化与反序列化(5)------反序列化时对象的创建方式