package com.lideng.work325;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
 * 序列化和反序列化
 * @author Administrator
 *
 */
public class Demo05  implements Serializable{

	public static void main(String[] args) {
		try {
			test01("c:/Test.java");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 反序列化
	 * @param file
	 * @throws Exception
	 */
	public static void test01(String file) throws Exception{
		ObjectInputStream os=new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
		Object obj = os.readObject();
		System.out.println(obj);
		int[] num=(int[])obj;
		for (int i = 0; i < num.length; i++) {
			System.out.println(num[i]);
		}
		
	}
	/**
	 * 序列化
	 * @param file
	 * @throws Exception
	 */
	public static void test(String file) throws Exception{
		
		int [] num={5,8,9,4,8};
		
		ObjectOutputStream os=new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream(file)));
		os.writeObject(num);
		os.flush();
		os.close();
	}
}