Advertisement
didito33

Main

Nov 21st, 2021
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. package file.example;
  2. import java.io.*;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. public class Main {
  7.  
  8. private static String dir = "src/";
  9. private static String file = "file";
  10.  
  11. public static void main(String[] args) {
  12. try (
  13. ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(dir.concat(file)));
  14. ObjectInputStream is = new ObjectInputStream(new FileInputStream(dir.concat(file)))) {
  15. List<Serializable> list = new ArrayList<>();
  16. list.add(new Individual("Deivid", 15));
  17. list.add(new Individual("Ivan", 18));
  18. list.add(new Individual("Petio", 15));
  19.  
  20. int counter = 0;
  21. for (Serializable el : list) {
  22. os.writeObject(el);
  23. counter++;
  24. }
  25. list.clear();
  26.  
  27. for (int i = 0; i < counter; i++) {
  28. Serializable inf = (Serializable) is.readObject();
  29. list.add(inf);
  30. }
  31.  
  32. list.forEach(System.out::println);
  33.  
  34. } catch (IOException |
  35. ClassNotFoundException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement