Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. import java.io.Serializable;
  2. public class Rectangle implements Serializable
  3. {
  4. private double length, width;
  5. public Rectangle(double l, double w)
  6. {
  7. length = l; width = w;
  8. }
  9. public void setDimension(double l, double w)
  10. {
  11. length = l; width = w;
  12. }
  13. public double getArea()
  14. {
  15. return length * width;
  16. }
  17. public String toString()
  18. {
  19. return length + " x " + width + " = " + getArea();
  20. }
  21. }
  22.  
  23. ============================================
  24. import java.io.*;
  25. import java.util.*;
  26. public class Main
  27. {
  28. public static void main(String[] args)
  29. {
  30.  
  31. }
  32. public static void readList()
  33. {
  34. try(ObjectInputStream is =
  35. new ObjectInputStream(
  36. new FileInputStream("list.dat")))
  37. {
  38. Object data = is.readObject();
  39. ArrayList<Rectangle> rList =
  40. (ArrayList<Rectangle>)data;
  41. Iterator<Rectangle> itor = rList.iterator();
  42. while (itor.hasNext())
  43. System.out.println(itor.next());
  44. }
  45. catch(FileNotFoundException e)
  46. {
  47. System.out.println(e);
  48. }
  49. catch(IOException | ClassNotFoundException e)
  50. {
  51. System.out.println(e);
  52. }
  53. }
  54. public static void saveList()
  55. {
  56. try(ObjectOutputStream os =
  57. new ObjectOutputStream(
  58. new FileOutputStream("list.dat")))
  59. {
  60. ArrayList<Rectangle> rList = new ArrayList();
  61. rList.add(new Rectangle(10, 1));
  62. rList.add(new Rectangle(20, 2));
  63. rList.add(new Rectangle(30, 3));
  64. os.writeObject(rList);
  65. }
  66. catch(FileNotFoundException e)
  67. {
  68. System.out.println(e);
  69. }
  70. catch(IOException e)
  71. {
  72. System.out.println(e);
  73. }
  74.  
  75. }
  76. public static void readRectangle()
  77. {
  78. try(ObjectInputStream is =
  79. new ObjectInputStream(
  80. new FileInputStream("object.dat")))
  81. {
  82. Rectangle r = (Rectangle)is.readObject();
  83. System.out.println(r);
  84.  
  85. System.out.println(is.readObject());
  86. System.out.println(is.readObject());
  87. // Testing
  88. r.setDimension(100, 20);
  89. System.out.println(r);
  90.  
  91. System.out.println("End of readRectangle");
  92. }
  93. catch(FileNotFoundException e)
  94. {
  95. System.out.println(e);
  96. }
  97. catch(IOException | ClassNotFoundException e)
  98. {
  99. System.out.println(e);
  100. }
  101. }
  102.  
  103. public static void saveRectangle()
  104. {
  105. try(ObjectOutputStream os =
  106. new ObjectOutputStream(
  107. new FileOutputStream("object.dat")))
  108. {
  109. os.writeObject(new Rectangle(10, 2));
  110. os.writeObject(new Rectangle(20, 4));
  111. os.writeObject(new Rectangle(30, 3));
  112. System.out.println("End of saveRectangle");
  113. }
  114. catch(FileNotFoundException e)
  115. {
  116. System.out.println(e);
  117. }
  118. catch(IOException e)
  119. {
  120. System.out.println(e);
  121. }
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement