자바 IO 대상 의 직렬 화 와 반 직렬 화

1078 단어 자바 학습
package io.ObjectStream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

import io.p2.bean.Person;



public class ObjectStreamDemo {

	public static void main(String[] args) throws IOException, ClassNotFoundException {
//		writeObJ();//                 
		readObj();//       .object      .class  
	}
	private 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();
	}
	private static void writeObJ() throws IOException, IOException {
		ObjectOutputStream oos=new ObjectOutputStream(	new FileOutputStream("obj.object"));
		//                 Serializable  		
		oos.writeObject(new Person("xiaoqiang", 30));
		
		oos.close();
	}
	
}

좋은 웹페이지 즐겨찾기