序列化和反序列化以及创建对象的几种方式
如何将对象中的信息永久保存
1.将来将对象信息存入数据库
2.java 中提供的序列化方式来永久保存数据
序列化流的概述
所谓的序列化:就是把对象通过流的方式存储到文件中.注意:此对象 要重写Serializable 接口才能被序列化
package org.westos.Demo;
import java.io.*;
public class Demo2 {
public static void main(String[] args) throws IOException {
ObjectOutputStream obji = new ObjectOutputStream(new FileOutputStream("Object1.txt"));
Teacher teacher = new Teacher();
teacher.setName("张三");
teacher.setAge(20);
obji.writeObject(teacher);
}
}
package org.westos.Demo;
import java.io.Serializable;
public class Teacher implements Serializable {
private String name;
public Teacher() {
}
public Teacher(String name, int age) {
this.name = name;
this.age = age;
}
private int age;
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;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
注:如果没有实现Serializable接口,会出现NotSerializableException
1)要求对象中的所有属性也都是可以序列化
2)如果某个属性不想序列化,可以在属性上加transient
关键字
反序列化:
把字节内容读取进来,还原为java对象
ObjectInputStream用来读取字节内容,还原(反序列化)为java对象
package org.westos.Demo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
public class Demo4 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream Obji = new ObjectInputStream(new FileInputStream("Object1.txt"));
Teacher t =(Teacher) Obji.readObject();
System.out.println(t.getName()+t.getAge());
}
}
除了可以写入和读取对象以外,还可以写入和读取基本类型(int,long,boolean...) ,读取和写入的顺序要保持一致
如果不一致,出现EOFException
如果没有更多内容,也会出现EOFException
建议在写入时最后一个对象使用null,这样读取时就可以根据null来判断是否读取完毕
序列化和反序列化其实也是java中的一种数据传输的机制
创建对象的几种方式:
1)new
2)克隆:clone()
3)反序列化
clone()举例
package org.westos.Demo;
public class Demo5 {
public static void main(String[] args) throws CloneNotSupportedException {
Teacher teacher1 = new Teacher();
teacher1.setName("李四");
teacher1.setAge(10);
System.out.println(teacher1.getName()+"----"+teacher1.getAge());
Teacher teacher2 = (Teacher)teacher1.clone();
System.out.println(teacher2.getName()+"---"+teacher2.getAge());
System.out.println(teacher1==teacher2);
}
}
package org.westos.Demo;
import java.io.Serializable;
public class Teacher implements Serializable,Cloneable {
private String name;
public Teacher() {
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public Teacher(String name, int age) {
this.name = name;
this.age = age;
}
private int age;
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;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
注: 克隆对应的设计模式:原型(prototype)模式 ,不走构造方法,根据一个已有对象创建新的对象。使用Cloneable接口和clone克隆的对象,仅仅是浅拷贝
,如果属性为引用类型,复制的仅是地址。没有为这个属性创建新的对象深拷贝
利用序列化和反序列化生成新的对象,也会为属性创建新的对象
深拷贝举例
package org.westos.Demo;
import java.io.*;
public class Demo2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectOutputStream objo = new ObjectOutputStream(new FileOutputStream("Object1.txt"));
Teacher teacher1 = new Teacher();
teacher1.setName("张三");
teacher1.setAge(20);
objo.writeObject(teacher1);
ObjectInputStream obji = new ObjectInputStream(new FileInputStream("Object1.txt"));
Teacher teacher2 = (Teacher) obji.readObject();
System.out.println(teacher2.getName()+"==="+teacher2.getAge());
System.out.println(teacher2 == teacher1);
}
}
ackage org.westos.Demo;
import java.io.Serializable;
public class Teacher implements Serializable,Cloneable {
private String name;
public Teacher() {
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public Teacher(String name, int age) {
this.name = name;
this.age = age;
}
private int age;
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;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}