目录
一、序列化
定义:
将内存中的Java对象保存到磁盘中或通过网络传输过去
方法:
使用过ObjectOutputStream类实现。
另外,ObjectOutputStream和ObjectInputStream不能序列化static和transient的成员变量
代码:
//序列化过程
@Test
public void test_Objectoutputstream(){
ObjectOutputStream oos= null;
try {
oos = new ObjectOutputStream(new FileOutputStream("D:\\Java develop\\Program\\superior_Java\\hello5.dat"));
String name="Ingram";
oos.writeObject(name);
oos.flush();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if(oos!=null) {
try {
oos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
二、反序列化
定义:
将对象从磁盘或者网络中读取到内存中(程序中)的过程
方法:
使用ObjectInputStream类实现
代码:
//反序列化过程
@Test
public void test_ObjectInputStream() {
ObjectInputStream ois= null;
try {
ois = new ObjectInputStream(new FileInputStream("D:\\Java develop\\Program\\superior_Java\\hello5.dat"));
Object s=ois.readObject();
String str=(String)s;
System.out.println(str);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} finally {
if(ois != null)
try {
ois.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
运行结果:
三、自定义类序列化
步骤:
要实现自定义类序列化:
1.需要去实现接口:Serializable
2.需要为当前类提供一个全局常量:SerializableUID
3.除了当前类Person需要实现Serializable接口之外,还必须保证内部所有属性也必须是可序列化的
代码:
先定义一个Person类
package Network_programming;
import java.io.Serializable;
/*要实现自定义类可序列化:
* 1.需要实现接口:Serializable
* 2.需要当前类提供一个全局常量:SerializableUID
* 3.除了当前类Person需要实现Serializable接口之外,还必须保证内部所有属性也必须是可序列化的
* */
public class Person implements Serializable {
public static final long serializableUID=23424324234L;
private String name;
private int number;
public Person(String name, int number) {
this.name = name;
this.number = number;
}
public Person() {
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return number
*/
public int getNumber() {
return number;
}
/**
* 设置
* @param number
*/
public void setNumber(int number) {
this.number = number;
}
public String toString() {
return "Person{name = " + name + ", number = " + number + "}";
}
}
序列化与反序列化实现:
package Network_programming;
import org.junit.Test;
import java.io.*;
public class demo2 {
public static void main(String[] args) {
test_Objectoutputstream();
test_ObjectInputStream();
}
public static void test_Objectoutputstream(){
ObjectOutputStream oos= null;
try {
oos = new ObjectOutputStream(new FileOutputStream("D:\\Java develop\\Program\\superior_Java\\hello6.dat"));
String name="Ingram";
//oos.flush();
oos.writeObject(name);
oos.writeObject(new Person("ingram",14));
oos.flush();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if(oos!=null) {
try {
oos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
//反序列化过程
public static void test_ObjectInputStream() {
ObjectInputStream ois= null;
try {
ois = new ObjectInputStream(new FileInputStream("D:\\Java develop\\Program\\superior_Java\\hello5.dat"));
Object s=ois.readObject();
String str=(String)s;
System.out.println(str);
System.out.println((Person)ois.readObject());
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} finally {
if(ois != null)
try {
ois.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
结果: