blob: e16be245ec67376a125672a158bfdc5d27f076e5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
/*
Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
Use and copying of this software and preparation of derivative works based
upon this software are permitted. Any distribution of this software or
derivative works must comply with all applicable United States export control
laws.
*/
package bean;
import java.beans.*;
import java.io.*;
public class Demo implements PropertyChangeListener {
static final String fileName = "test.tmp";
/**
* when Demo is playing the listener role,
* this method reports that a propery has changed
*/
public void propertyChange(PropertyChangeEvent e){
System.out.println("Property " + e.getPropertyName() + " changed from " +
e.getOldValue() + " to " + e.getNewValue() );
}
/**
* main: test the program
*/
public static void main(String[] args){
Point p1 = new Point();
p1.addPropertyChangeListener(new Demo());
System.out.println("p1 =" + p1);
p1.setRectangular(5,2);
System.out.println("p1 =" + p1);
p1.setX( 6 );
p1.setY( 3 );
System.out.println("p1 =" + p1);
p1.offset(6,4);
System.out.println("p1 =" + p1);
save(p1, fileName);
Point p2 = (Point) restore(fileName);
System.out.println("Had: " + p1);
System.out.println("Got: " + p2);
}
/**
* Save a serializable object to a file
*/
static void save(Serializable p, String fn){
try {
System.out.println("Writing to file: " + p);
FileOutputStream fo = new FileOutputStream(fn);
ObjectOutputStream so = new ObjectOutputStream(fo);
so.writeObject(p);
so.flush();
} catch (Exception e) {
System.out.println(e);
System.exit(1);
}
}
/**
* Restore a serializable object from the file
*/
static Object restore(String fn){
try {
Object result;
System.out.println("Reading from file: " + fn);
FileInputStream fi = new FileInputStream(fn);
ObjectInputStream si = new ObjectInputStream(fi);
return si.readObject();
} catch (Exception e) {
System.out.println(e);
System.exit(1);
}
return null;
}
}
|