Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. package menu;
  2.  
  3. import java.io.*;
  4.  
  5. class Cat implements Serializable {
  6.  
  7. /**
  8. *
  9. */
  10. private static final long serialVersionUID = -6226623636109879891L;
  11. String teste = "sfasdfsfd";
  12. } // 1
  13.  
  14. class SerializeCat {
  15. public static void main(String args[]) {
  16. Cat c = new Cat(); // criando o objeto
  17.  
  18. // aqui vou serializar meu objeto da class cat
  19. try {
  20. FileOutputStream fo = new FileOutputStream("test.ser");
  21. ObjectOutputStream oo = new ObjectOutputStream(fo);
  22. oo.writeObject(c); // serializo objeto cat
  23. oo.close();
  24. System.out.println("Class Cat - object serializado com sucesso");
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. }
  28. // des-serializo o objeto
  29. try {
  30. FileInputStream fi = new FileInputStream("test.ser");
  31. ObjectInputStream oi = new ObjectInputStream(fi);
  32. c = (Cat) oi.readObject();
  33. oi.close();
  34. System.out.println("agora ele foi des-serializado com sucesso");
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement