java对象序列化实例_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java对象序列化实例

java对象序列化实例

 2013/10/24 12:53:13  alleni123  程序员俱乐部  我要评论(0)
  • 摘要:packagecom.lj.serialize;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.ObjectInputStream;importjava.io.ObjectOutputStream;importjava.io.Serializable;publicclassSerializableTest1
  • 标签:Java 实例 序列化
class="java">package com.lj.serialize;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class SerializableTest1
{
	public static void main(String[] args) throws IOException, ClassNotFoundException
	{
		Person p1=new Person(25,"jack",5);
		Person p2=new Person(27,"mike",4);
		Person p3=new Person(50,"joe",4);
	
		FileOutputStream fos=new FileOutputStream("person.txt");
		
		ObjectOutputStream oos=new ObjectOutputStream(fos);
		
		oos.writeObject(p1);
		oos.writeObject(p2);
		oos.writeObject(p3);
		
		oos.close();
		
		
		
		System.out.println("==============");
		FileInputStream fis=new FileInputStream("person.txt");
		
		ObjectInputStream ois=new ObjectInputStream(fis);
		
		Person p=null;
		for(int i=0;i<3;i++){
			p=(Person) ois.readObject();
			System.out.println(p.name+" "+p.age);
		}
		
	}
}


class Person implements Serializable{
	int age;
	
	String name;
	
	double height;

	public Person(int age, String name, double height)
	{
		this.age = age;
		this.name = name;
		this.height = height;
	}
	
	
	
	
	
}
发表评论
用户名: 匿名