選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Demo.java 2.3KB

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