Guest User

Untitled

a guest
May 25th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. import java.io.*;
  2. public class SerializableDemo_try1 implements Serializable {
  3. String s;
  4. int n;
  5. public SerializableDemo_try1(String s, int n) {
  6. this.s = s;
  7. this.n = n;
  8. }
  9. public static void main(String args[]) {
  10. try {
  11. SerializableDemo_try1 object_Original = new SerializableDemo_try1("---Hello---", 3);
  12.  
  13. FileOutputStream fos = new FileOutputStream("object.dat");
  14. ObjectOutputStream oos = new ObjectOutputStream(fos);
  15. oos.writeObject(object_Original);
  16. oos.writeInt(111111111);
  17. oos.writeInt(2);
  18. oos.writeObject("HelloWorld");
  19. oos.writeFloat(2.0f);
  20. oos.flush();
  21. //把所有緩衝的物件全部寫入檔案
  22. oos.close();
  23. fos.close();
  24.  
  25. FileInputStream fis = new FileInputStream("object.dat");
  26. ObjectInputStream ois = new ObjectInputStream(fis);
  27. SerializableDemo_try1 object_getAgain =(SerializableDemo_try1) ois.readObject();
  28. int i = ois.readInt();
  29. Object str = ois.readObject();
  30. float j = ois.readFloat();
  31. ois.close();
  32. fis.close();
  33.  
  34. System.out.println(
  35. "object_Original: s=" + object_Original.s + " ; n=" + object_Original.n);
  36. System.out.println(
  37. "object_getAgain: s=" + object_getAgain.s + " ; n=" + object_getAgain.n);
  38. System.out.println("i=" + i);
  39. System.out.println("str=" + str);
  40. System.out.println("j=" + j);
  41. } catch (Exception e) {
  42. System.out.println(e.getMessage());
  43. e.printStackTrace();
  44. }
  45. }
  46. }
Add Comment
Please, Sign In to add comment