序列化: 将对象按字节顺序进行存储
反序列化: 在需要的时候将序列化的对象还原
实现序列化接口Serializable,才能启用序列化功能。
//读,反序列化
private static void read() throws Exception{
FileInputStream fis = new FileInputStream("out.object");
ObjectInputStream in = new ObjectInputStream(fis);
Person p = (Person)in.readObject();
System.out.println(p);
}
//写,序列化
private static void write() throws Exception {
FileOutputStream fos = new FileOutputStream("out.object");
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(new Person("test",12));
fos.close();
out.close();
}
序列化例子
import java.io.*;
import java.util.*;
public class Test{
public static boolean test(String path){
Person p = new Person("张三", 21, 60f);
Person p2 = new Person("李四", 26, 70f);
Person p3 = new Person("王五", 18, 58f);
FileOutputStream fos = null;
BufferedOutputStream bos = null;
ObjectOutputStream oos = null;
try{
fos = new FileOutputStream(path);
bos = new BufferedOutputStream(fos);
oos = new ObjectOutputStream(bos);
oos.writeObject(p);
oos.writeObject(p2);
oos.writeObject(p3);
return true;
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(oos != null){
oos.close();
}
if(bos != null){
bos.close();
}
if(fos != null){
fos.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
return false;
}
public static boolean test(String path, Object obj){
FileOutputStream fos = null;
BufferedOutputStream bos = null;
ObjectOutputStream oos = null;
try{
fos = new FileOutputStream(path);
bos = new BufferedOutputStream(fos);
oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
return true;
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(oos != null){
oos.close();
}
if(bos != null){
bos.close();
}
if(fos != null){
fos.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
return false;
}
public static void main(String[] args){
String path = "./testPersonSeria.dat";
boolean flag = test(path);
if(flag){
System.out.println("序列化成功");
}else{
System.out.println("序列化失败");
}
Person p = new Person("张三", 21, 60f);
Person p2 = new Person("李四", 26, 70f);
Person p3 = new Person("王五", 18, 58f);
Person[] persons = {p, p2, p3};
String arrayPath = "./testPersonArraySeria.dat";
flag = test(arrayPath, persons);
if(flag){
System.out.println("Person数组序列化成功");
}else{
System.out.println("Person数组序列化失败");
}
List<Person> personList = new ArrayList<Person>();
String listPath = "./testPersonListSeria.dat";
personList.add(p);
personList.add(p2);
personList.add(p3);
flag = test(listPath, personList);
if(flag){
System.out.println("Person集合序列化成功");
}else{
System.out.println("Person集合序列化失败");
}
}
}
import java.io.*;
import java.util.*;
public class Test2{
public static void read(String path){
FileInputStream fis = null;
BufferedInputStream bis = null;
ObjectInputStream ois = null;
try{
fis = new FileInputStream(path);
bis = new BufferedInputStream(fis);
ois = new ObjectInputStream(bis);
Person p = (Person)ois.readObject();
Person p2 = (Person)ois.readObject();
Person p3 = (Person)ois.readObject();
System.out.println(p);
System.out.println(p2);
System.out.println(p3);
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(ois != null){
ois.close();
}
if(bis != null){
bis.close();
}
if(fis != null){
fis.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
public static Object readMethod(String path){
FileInputStream fis = null;
BufferedInputStream bis = null;
ObjectInputStream ois = null;
try{
fis = new FileInputStream(path);
bis = new BufferedInputStream(fis);
ois = new ObjectInputStream(bis);
return ois.readObject();
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(ois != null){
ois.close();
}
if(bis != null){
bis.close();
}
if(fis != null){
fis.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
return null;
}
@SuppressWarnings("unchecked")
public static void main(String[] args){
String path = "./testPersonSeria.dat";
read(path);
String arrayPath = "./testPersonArraySeria.dat";
String listPath = "./testPersonListSeria.dat";
Person[] persons = (Person[])readMethod(arrayPath);
System.out.println("===========array===========");
for(Person p : persons){
System.out.println(p);
}
System.out.println("===========list===========");
List<Person> personList = (List<Person>)readMethod(listPath);
for(Person p : personList){
System.out.println(p);
}
}
}