흑마 프로그래머 24-2: 서열화 및 반서열화

2851 단어 프로그래머
-------
android 교육
4java 교육 당신과 교류하기를 기대합니다!

package cn.itcast.io.p2.objectstream;

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 cn.itcast.io.p2.bean.Person;

public class ObjectStreamDemo {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws IOException, ClassNotFoundException {
			
//		writeObj();
		readObj();
	}

	public static void readObj() throws IOException, ClassNotFoundException {
		
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.object"));
		//       。 
		Person p = (Person)ois.readObject();
		
		System.out.println(p.getName()+":"+p.getAge());
		
		ois.close();
		
	}

	public static void writeObj() throws IOException, IOException {
		
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.object"));
		//     。             Serializable  。 
		oos.writeObject(new Person("  ",30));
		
		oos.close();
		
		
		
	}

}

package cn.itcast.io.p2.bean;

import java.io.Serializable;
/*
 * Serializable:           ID 。
 *                 。 
 */
public class Person implements Serializable/*    */ {

	/**
	 * transient:                      。transient,  
	 *            
	 */
	private static final long serialVersionUID = 9527l;
	private transient String name;
	private static int age;
	
	public Person(String name, int age) {
		super();
		this.name = name;
		this.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;
	}
	
}


-------
android 교육
4java 교육 당신과 교류하기를 기대합니다!
자세한 내용은 다음을 참조하십시오.
http://edu.csdn.net/heima -------

좋은 웹페이지 즐겨찾기