3.对象的序列化,反序列化
1)对象序列化,就是将Object转换成byte序列,反之叫对象的反序列化
2)序列化流(ObjectOutputStream),是过滤流----writeObject
反序列化流(ObjectInputStream)---readObject
3)序列化接口(Serializable)
对象必须实现序列化接口 ,才能进行序列化,否则将出现异常
这个接口,没有任何方法,只是一个标准
4) transient关键字
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException
分析ArrayList源码中序列化和反序列化的问题
如果变量被transient关键字修饰 和static修饰是不会被自动序列化的 需要手动操作:
===============================================================================
1)对象序列化,就是将Object转换成byte序列,反之叫对象的反序列化
2)序列化流(ObjectOutputStream),是过滤流----writeObject
反序列化流(ObjectInputStream)---readObject
3)序列化接口(Serializable)
对象必须实现序列化接口 ,才能进行序列化,否则将出现异常
这个接口,没有任何方法,只是一个标准
4) transient关键字
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException
分析ArrayList源码中序列化和反序列化的问题
5)序列化中 子类和父类构造函数的调用问题
对子类对象进行反序列化操作时,如果一个父类没有实现Serializable接口就会调用父类的默认构造器
如果父类实现了Serializable接口子类也可以进行序列化操作
================================================
测试类:
public class SerDemo {
/**
* 3.对象的序列化,反序列化 1)对象序列化,就是将Object转换成byte序列,反之叫对象的反序列化
* 2)序列化流(ObjectOutputStream),是过滤流----writeObject
* 反序列化流(ObjectInputStream)---readObject
*
* 3)序列化接口(Serializable) 对象必须实现序列化接口 ,才能进行序列化,否则将出现异常 这个接口,没有任何方法,只是一个标准
*
* @param args
* @throws FileNotFoundException
* @throws IOException
* @throws ClassNotFoundException
* @throws Exception
*/
// 对象序列化
public void ObjectOutputStream1(String pathname)
throws FileNotFoundException, IOException {
ObjectOutputStream oso = new ObjectOutputStream(new FileOutputStream(
pathname));
Good g = new Good("这个杀手不怕冷", 300000000, "你想要什么?");
oso.writeObject(g);
oso.flush();
oso.close();
}
// 对象反序列化
public void ObjectInputStream1(String st) throws IOException, Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(st));
Good b = (Good) ois.readObject();
System.out.println(b);
ois.close();
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String s = "demo\\ser.dat";
SerDemo ser = new SerDemo();
// ser.ObjectOutputStream1(s);
// ser.ObjectInputStream1(s);
}
实体类:
public class Good implements Serializable {
private String name;
private int age;
private String description;
public Good(){
}
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 getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Good(String name, int age, String description) {
super();
this.name = name;
this.age = age;
this.description = description;
}
@Override
public String toString() {
return "Good [name=" + name + ", age=" + age + ", description="
+ description + "]";
}
}
如果变量被transient关键字修饰 和static修饰是不会被自动序列化的 需要手动操作:
如何实现手动操作 需要在实体类覆盖ArrayList的
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException
---------------------------------------------------------------------------------------------------------------
实体类:
package Serializable2;
import java.io.Serializable;
public class Student implements Serializable {
private String name;
private transient String description;
private transient int age;
public Student(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", description=" + description + ", age="
+ age + "]";
}
public Student(String name, String description, int age) {
super();
this.name = name;
this.description = description;
this.age = age;
}
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException{
s.defaultWriteObject();
int a=description.length();
s.writeInt(a);
for(int i=0;i<a;i++){
s.writeChar(description.charAt(i));
}
s.writeInt(age);
}
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException{
s.defaultReadObject();
// int b=description.length();//错误 还没读取jvm怎么知道description.length()是什么东西
int b=s.readInt();
char [] c=new char[b];
for(int i=0;i<b;i++){
c[i]=s.readChar();
this.description=new String(c,0,b);
}
this.age=s.readInt();
}
}
测试类与上面无异:
package Serializable2;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SerDemo3 {
/**transient关键字 变量就不再进行jvm的默认的序列化操作 但是可以自己进行序列化操作
* @param args
* @throws Exception
*/
//对象序列化
public void ObjectOutputStream1(String pathname) throws FileNotFoundException, IOException{
ObjectOutputStream oso=new ObjectOutputStream(
new FileOutputStream (pathname));
Student g=new Student("这个杀手很怕冷","怕是不冷喔",18);
oso.writeObject(g);
oso.flush();
oso.close();
}
//对象反序列化
public void ObjectInputStream1(String st) throws IOException, Exception{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(st));
Student b = (Student) ois.readObject();
System.out.println(b);
ois.close();
}
public static void main(String[] args) throws Exception {
String s="demo\\ser3.dat";
SerDemo3 ser3=new SerDemo3();
ser3.ObjectOutputStream1(s);
ser3.ObjectInputStream1(s);
}
}
===============================================================================
找到的对象序列化好文章:http://www.cnblogs.com/lanxuezaipiao/p/3369962.html
public class ObjectSeriaDemo2 {
public static void main(String[] args) throws Exception{
/*ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("demo/obj1.dat"));
Foo2 foo2 = new Foo2();
oos.writeObject(foo2);
oos.flush();
oos.close();*/
//反序列化是否递归调用父类的构造函数
/*ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("demo/obj1.dat"));
Foo2 foo2 = (Foo2)ois.readObject();
System.out.println(foo2);
ois.close();*/
/*ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("demo/obj1.dat"));
Bar2 bar2 = new Bar2();
oos.writeObject(bar2);
oos.flush();
oos.close();*/
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("demo/obj1.dat"));
Bar2 bar2 = (Bar2)ois.readObject();
System.out.println(bar2);
ois.close();
/*
* 对子类对象进行反序列化操作时,
* 如果其父类没有实现序列化接口
* 那么其父类的构造函数会被调用
*/
}
}
/*
* 一个类实现了序列化接口,那么其子类都可以进行序列化
*/
class Foo implements Serializable{
public Foo(){
System.out.println("foo...");
}
}
class Foo1 extends Foo{
public Foo1(){
System.out.println("foo1...");
}
}
class Foo2 extends Foo1{
public Foo2(){
System.out.println("foo2...");
}
}
class Bar{
public Bar(){
System.out.println("bar");
}
}
class Bar1 extends Bar{
public Bar1(){
System.out.println("bar1..");
}
}
class Bar2 extends Bar1 implements Serializable{
public Bar2(){
System.out.println("bar2...");
}
}