Advertisement
Guest User

java ser and des

a guest
Dec 13th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Colegiu implements Serializable {
  5.  
  6. public static void main(String[] args) throws ClassNotFoundException {
  7.  
  8. int i;
  9. Item[] items = new Item[2];
  10.  
  11. Colegiu c = new Colegiu();
  12. for (i = 0; i < items.length; i++) {
  13.  
  14. items[i] = c.new Item();
  15. }
  16.  
  17. items[0].setItemID("ITEM101");
  18. items[1].setItemID("ITEM102");
  19.  
  20. items[0].setDesc("iPad");
  21. items[1].setDesc("iPhone");
  22.  
  23. items[0].setCost(499);
  24. items[1].setCost(599);
  25.  
  26. items[0].setQuantity(1);
  27. items[1].setQuantity(3);
  28.  
  29. System.out.println("Item Detalii.....");
  30. for (Item d : items) {
  31. System.out.print(d.getItemID());
  32. System.out.print("\t" + d.getDesc());
  33. System.out.print("\t" + d.getCost());
  34. System.out.println("\t" + d.getQuantity());
  35. }
  36.  
  37. List<Item> obj;
  38. obj = new ArrayList<Item>();
  39.  
  40. for (i = 0; i < items.length; i++) {
  41. obj.add(items[i]);
  42. }
  43.  
  44. // Sa facem serializarea unui obiect
  45. try {
  46. FileOutputStream fileOut = new FileOutputStream("out.txt");
  47. ObjectOutputStream out = new ObjectOutputStream(fileOut);
  48. out.writeObject(obj);
  49. out.close();
  50. fileOut.close();
  51. System.out.println("\nSerializare Succes ... Verificati fisierul de iesire specificat ..\n");
  52.  
  53. } catch (FileNotFoundException e) {
  54. e.printStackTrace();
  55. } catch (IOException e) {
  56. e.printStackTrace();
  57. }
  58.  
  59. // Aici deserializam un Obiect
  60. try {
  61. FileInputStream fileIn = new FileInputStream("out.txt");
  62. ObjectInputStream in = new ObjectInputStream(fileIn);
  63. System.out.println("Datele deserializate: \n" + in.readObject().toString());
  64. in.close();
  65. fileIn.close();
  66. } catch (FileNotFoundException e) {
  67. e.printStackTrace();
  68. } catch (IOException e) {
  69. e.printStackTrace();
  70. }
  71. }
  72.  
  73. public class Item implements Serializable {
  74.  
  75. private String itemID;
  76. private String desc;
  77. private double cost;
  78. private int quantity;
  79.  
  80. public Item() {
  81. itemID = "";
  82. desc = "";
  83. cost = 0;
  84. quantity = 0;
  85. }
  86.  
  87. public Item(String id, String d, double c, int q) {
  88. itemID = id;
  89. desc = d;
  90. cost = c;
  91. quantity = q;
  92.  
  93. }
  94.  
  95. public String getItemID() {
  96. return itemID;
  97. }
  98.  
  99. public void setItemID(String itemID) {
  100. this.itemID = itemID;
  101. }
  102.  
  103. public String getDesc() {
  104. return desc;
  105. }
  106.  
  107. public void setDesc(String desc) {
  108. this.desc = desc;
  109. }
  110.  
  111. public double getCost() {
  112. return cost;
  113. }
  114.  
  115. public void setCost(double cost) {
  116. this.cost = cost;
  117. }
  118.  
  119. public int getQuantity() {
  120. return quantity;
  121. }
  122.  
  123. public void setQuantity(int quantity) {
  124. this.quantity = quantity;
  125. }
  126.  
  127. public String toString() {
  128. return "Item [itemID=" + itemID + ", desc=" + desc + ", cost=" + cost + ", quantity=" + quantity + "]";
  129. }
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement