序列化与反序列化

序列化: 将对象按字节顺序进行存储
反序列化: 在需要的时候将序列化的对象还原

实现序列化接口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);
		}
		
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值