Java 序列化与反序列化练习

import java.io.*;
//电影类型
enum MovieType implements Serializable{
	Comedy("喜剧片"),
	Action("动作片"),
	Romatic("爱情片"),
	ScienceFiction("科幻片");
	private final String name;
	MovieType(String name){
		this.name = name;
	}
	public String get(){
		return name;
	}
	
}
//电影
class Movie implements Serializable{
	String name;
	MovieType type;
	String where;
	Movie(String where,String name,MovieType a){
		this.name = name;
		this.where = where;
		this.type = a;
	}
	void printinfo(){
		System.out.print(this.name+"\n"+"类型:"+this.type.get()+"\n"+"放映厅:"+this.where+"\n");
	}
}
//电影放映时间段
class MovieSchedule implements Serializable{
	String time;
	String date;
	MovieSchedule(String date,String time){
		this.time = time;
		this.date = date;
	}
	void printinfo(){
		System.out.print("时间:"+this.date+" "+this.time+"\n");
	}
}
//座位
class Seat implements Serializable{
	int row;
	int col;
	Seat(int a,int b){
		this.row = a;
		this.col = b;
	}
	void printinfo(){
		System.out.print("座位:"+this.row+"行"+this.col+"座\n");
	}
}
//电影票
class Ticket implements Serializable{
	Movie a;
	MovieSchedule b;
	Seat c;
	Ticket(Movie get,MovieSchedule time,Seat seat){
		this.a = get;
		this.b = time;
		this.c = seat;
	}
	public void printTicket(){ //打印票的详细信息
		a.printinfo();
		b.printinfo();
		c.printinfo();
	}
}
public class MovieSet {
	public static void main (String[] args) {
		Seat seat = new Seat(10,20);
		MovieSchedule time = new MovieSchedule("2007年11月3日","11时20分");
		Movie get = new Movie("放映厅1","疯狂的石头",MovieType.Comedy);
		Ticket one = new Ticket(get,time,seat);
		one.printTicket();
		try{
			FileOutputStream fs = new FileOutputStream("foo.ser");  
			ObjectOutputStream os = new ObjectOutputStream(fs);  
			os.writeObject(one);
			os.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		try{
			ObjectInputStream in = new ObjectInputStream(new FileInputStream("foo.ser"));
			Ticket getit = (Ticket) in.readObject();
			getit.printTicket();
			in.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}

  

 

利用Date类型做成的反序列化功能,Date和SimpleDateFormat

 1 import java.io.*;
2 import java.util.*;
3 import java.text.SimpleDateFormat;
4 //电影类型
5 enum MovieType implements Serializable{
6 Comedy("喜剧片"),
7 Action("动作片"),
8 Romatic("爱情片"),
9 ScienceFiction("科幻片");
10 private final String name;
11 MovieType(String name){
12 this.name = name;
13 }
14 public String get(){
15 return name;
16 }
17
18 }
19 //电影
20 class Movie implements Serializable{
21 String name;
22 MovieType type;
23 String where;
24 Movie(String where,String name,MovieType a){
25 this.name = name;
26 this.where = where;
27 this.type = a;
28 }
29 void printinfo(){
30 System.out.print(this.name+"\n"+"类型:"+this.type.get()+"\n"+"放映厅:"+this.where+"\n");
31 }
32 }
33 //电影放映时间段
34 class MovieSchedule implements Serializable{
35 Date date;
36 SimpleDateFormat set = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
37 MovieSchedule(){
38 this.date = new Date();
39 };
40 void printinfo(){
41 System.out.print("时间:"+set.format(this.date)+"\n");
42 }
43 }
44 //座位
45 class Seat implements Serializable{
46 int row;
47 int col;
48 Seat(int a,int b){
49 this.row = a;
50 this.col = b;
51 }
52 void printinfo(){
53 System.out.print("座位:"+this.row+"行"+this.col+"座\n");
54 }
55 }
56 //电影票
57 class Ticket implements Serializable{
58 Movie a;
59 MovieSchedule b;
60 Seat c;
61 Ticket(Movie get,MovieSchedule time,Seat seat){
62 this.a = get;
63 this.b = time;
64 this.c = seat;
65 }
66 public void printTicket(){ //打印票的详细信息
67 a.printinfo();
68 b.printinfo();
69 c.printinfo();
70 }
71 }
72 public class MovieSet {
73 public static void main (String[] args) {
74 Seat seat = new Seat(10,20);
75 MovieSchedule time = new MovieSchedule();
76 Movie get = new Movie("放映厅1","疯狂的石头",MovieType.Comedy);
77 Ticket one = new Ticket(get,time,seat);
78 one.printTicket();
79 try{
80 FileOutputStream fs = new FileOutputStream("foo.ser");
81 ObjectOutputStream os = new ObjectOutputStream(fs);
82 os.writeObject(one);
83 os.close();
84 }catch(Exception ex){
85 ex.printStackTrace();
86 }
87 try{
88 ObjectInputStream in = new ObjectInputStream(new FileInputStream("foo.ser"));
89 Ticket getit = (Ticket) in.readObject();
90 getit.printTicket();
91 in.close();
92 }catch(Exception ex){
93 ex.printStackTrace();
94 }
95 }
96 }



posted on 2012-03-20 09:09  小影帆  阅读(501)  评论(0编辑  收藏  举报

导航