자바 직렬 화 흐름: 직렬 화 와 역순 화

3518 단어 자바
package com.qianfeng.test;

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;

/*
 *     :                 
 * 
 *     :        ,             ----  ,  ,  ,  
 *     :        ,       ,      ----  
 * 
 *    :          ,            .
 *     :          .
 * 
 *         :---            
 * 1.     
 * 2.             ---   ---ObjectOutputStream
 * 3.                   ---    -----ObjectInputStream
 * 4.   
 * 
 *   :                   
 * 
//        
//      java.io.Serializable            。                         。
//                    。            ,            。
/*
 *   :
 *          Serializable,         :NotSerializableException
 * 
 *    Serializable           :
 * 1.       
 * 2.                  
 * 3.           ,             
 * 
 * *    :
 * 1. ClassNotFoundException:        
 *   : Person         , Person   ,               
 *   :                 ,     ,         ,      
 * 
 * 2.java.io.InvalidClassException      
 *      :       serialVersionUID,      .          ,     ,        
 *          (      id       ,   Person   id),      
 * 
 * 3.     serialVersionUID     ID   ?
 *      ,        ,id      ,            ,        ,id     
 *       ,        ,id      ,          ,   Person        ,        
 * 
 * 4.     ,          :
 * a.              ,             
 * b.            Serializable  ,             .    ,    
 * c.                      Serializable  
 * 
 */
public class Demo11 {
     public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
    	 //   
    	 fun1();
		 //    
    	 fun2();
	}
     
     //   
     public static void fun1() throws FileNotFoundException, IOException{
    	 //           
 		ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("temp4.txt"));
 		//     
 		//  :               ,          
 		objectOutputStream.writeObject(new Person("bingbing",20));
 		//   
 		objectOutputStream.close();
     }
     //    
     public static void fun2() throws FileNotFoundException, IOException, ClassNotFoundException {
    	//            
  		ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("temp4.txt"));
  		
  		//      -- 
  		Object object = objectInputStream.readObject();
  		Person person = (Person)object;
  		System.out.println(person);
  		
  		//   
  		objectInputStream.close();
	}
}
//     
//                 Serializable  .
//     Serializable         .                    .
//      Serializable,    NoSerializableException
//   :                     .
class Person implements Serializable{
/**
 * generated:         ,   L  long   
 * 
 *  */
	private static final long serialVersionUID = 9100147150931711124L;
	//	/**
//	 * default:     ,         ,    1L
//	 */
//	private static final long serialVersionUID = 17489587495847L;
	String name;
	int age;
	//int height;
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public Person() {
		super();
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + 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;
	}
}

좋은 웹페이지 즐겨찾기