fork download
  1. import java.io.*;
  2. class serialization {
  3. public static void main(String[] args) {
  4. try {
  5. Myclass object1 = new Myclass("Hello", -7, 2.1e10);
  6. FileOutputStream fos = new FileOutputStream("serial");
  7. oos.writeObject(object1);
  8. oos.flush();
  9. oos.close();
  10. }
  11. catch(Exception e) {
  12. System.out.println("Serialization" + e);
  13. System.exit(0);
  14. }
  15. try {
  16. int x;
  17. FileInputStream fis = new FileInputStream("serial");
  18. x = ois.readInt();
  19. ois.close();
  20. System.out.println(x);
  21. }
  22. catch (Exception e) {
  23. System.out.print("deserialization");
  24. System.exit(0);
  25. }
  26. }
  27. }
  28. class Myclass implements Serializable {
  29. String s;
  30. int i;
  31. double d;
  32. Myclass(String s, int i, double d){
  33. this.d = d;
  34. this.i = i;
  35. this.s = s;
  36. }
  37. }
  38.  
Success #stdin #stdout 0.17s 47992KB
stdin
Standard input is empty
stdout
Serializationjava.io.FileNotFoundException: serial (Permission denied)