Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. Demo object1 = new Demo(100,"saran");
  2. Demo object2 = new Demo(20 ,"nivas");
  3.  
  4. ArrayList<Demo> list=new ArrayList<>();
  5. try {
  6. FileOutputStream file=new FileOutputStream("C:\Users\SARANNIVAS\Documents\file\obj");
  7. ObjectOutputStream out=new ObjectOutputStream(file);
  8. list.add(object1);
  9. list.add(object2);
  10. out.writeObject(list);
  11.  
  12. out.close();
  13. file.close();
  14.  
  15. System.out.println("object has been serialized");
  16.  
  17. } catch(IOException ex)
  18. {
  19. ex.printStackTrace();
  20. System.out.println("exception is caught");
  21. }
  22.  
  23.  
  24. try {
  25. FileInputStream file=new FileInputStream("C:\Users\SARANNIVAS\Documents\file\obj");
  26. ObjectInputStream in=new ObjectInputStream(file);
  27. ArrayList<Demo> list1=new ArrayList<>();
  28. list1=(ArrayList<Demo>)in.readObject();
  29.  
  30. for(Demo d:list1) {
  31. System.out.println(d.a);
  32. System.out.println(d.b);
  33. }
  34.  
  35. } catch(IOException ex) {
  36. System.out.println("IOException is caught");
  37. }
  38.  
  39. public static void main(String... args) {
  40. Demo object1 = new Demo(100, "saran");
  41. Demo object2 = new Demo(20, "nivas");
  42.  
  43. serialize(object1, object2);
  44.  
  45. List<Demo> collections = load();
  46.  
  47. //search
  48. List<Demo> result = search(collections, "saran");
  49. //match result
  50. if (Objects.nonNull(result))
  51. result.stream().forEach(System.out::println);
  52.  
  53. }
  54.  
  55. public static List<Demo> search(List<Demo> collections, String name) {
  56. if (Objects.isNull(collections) || collections.isEmpty()) return null;
  57. return collections.stream().filter(itm -> itm.b.equals(name)).collect(Collectors.toList());
  58. }
  59.  
  60. public static void serialize(Demo... objects) {
  61. try {
  62. FileOutputStream file = new FileOutputStream("/home/jst/obj");
  63. ObjectOutputStream out = new ObjectOutputStream(file);
  64. if (Objects.nonNull(objects)) {
  65. List<Demo> collect = Arrays.stream(objects).collect(Collectors.toList());
  66. out.writeObject(collect);
  67. out.close();
  68. file.close();
  69. System.out.println("object has been serialized");
  70. }
  71. } catch (
  72. IOException ex) {
  73. ex.printStackTrace();
  74. System.out.println("exception is caught");
  75. }
  76. }
  77.  
  78. public static List<Demo> load() {
  79. try {
  80. FileInputStream file = new FileInputStream("/home/jst/obj");
  81. ObjectInputStream in = new ObjectInputStream(file);
  82. return (ArrayList<Demo>) in.readObject();
  83. } catch (Exception ex) {
  84. System.out.println("IOException is caught");
  85. return null;
  86. }
  87. }
  88.  
  89.  
  90. public static class Demo implements Serializable {
  91. private int a;
  92. private String b;
  93.  
  94. public Demo(int a, String b) {
  95. this.a = a;
  96. this.b = b;
  97. }
  98.  
  99. public int getA() {
  100. return a;
  101. }
  102.  
  103. public void setA(int a) {
  104. this.a = a;
  105. }
  106.  
  107. public String getB() {
  108. return b;
  109. }
  110.  
  111. public void setB(String b) {
  112. this.b = b;
  113. }
  114.  
  115. @Override
  116. public String toString() {
  117. return "Demo{" +
  118. "a=" + a +
  119. ", b='" + b + ''' +
  120. '}';
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement