Yurttas/PL/OOL/Java/F/06/03/00/Serialization00.java

From ZCubes Wiki
Jump to navigation Jump to search
 1/**
 2 *  Copyright(C) 1998
 3 *  All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
 4 *
 5 *  Permission to use, copy, modify, and distribute this
 6 *  software and its documentation for EDUCATIONAL purposes
 7 *  and without fee is hereby granted provided that this
 8 *  copyright notice appears in all copies.
 9 *
10 *  @date   : January 1, 1998.
11 *  @author : Salih Yurttas.
12 */
13
14
15import java.io.*;
16
17public class Serialization00 {
18
19  public static void main(String args[]) {
20    Serialization00 s00 = new Serialization00();
21
22    s00.writeData();
23    s00.readData();
24  }
25
26  public void writeData() {
27    try {
28      FileOutputStream fOS = new FileOutputStream("objects.dat");
29      ObjectOutputStream oOS = new ObjectOutputStream(fOS);
30
31      String[] givenStrings = {"tiger",
32                               "lion",
33                               "monkey",
34                               "elephant"};
35      long num = 1998;
36
37      int[] givenInts = {4, 1, 7, 9, 5, 8};
38
39      String anyString = givenStrings[3];
40
41      oOS.writeObject(givenStrings);
42      oOS.writeLong(num);
43      oOS.writeObject(givenInts);
44      oOS.writeObject(anyString);
45
46      oOS.flush();
47      oOS.close();
48    }
49    catch(IOException iOE) {
50      System.err.println(iOE);
51    }
52  }
53
54  void readData() {
55    try {
56      FileInputStream fIS = new FileInputStream("objects.dat");
57      ObjectInputStream oIS = new ObjectInputStream(fIS);
58
59      String[] givenStrings = (String[])oIS.readObject();
60
61      long num = oIS.readLong();
62
63      int[] givenInts = (int[])oIS.readObject();
64
65      String anyString = (String)oIS.readObject();
66
67      for(int i=0; i<givenStrings.length; i++)
68        System.out.print(givenStrings[i] + "\t");
69
70      System.out.println();
71
72      System.out.println(num);
73
74      for(int i=0; i<givenInts.length; i++)
75        System.out.print(givenInts[i] + "\t");
76
77      System.out.println();
78
79      System.out.println(anyString);
80
81      oIS.close();
82    }
83    catch(Exception e) {
84      System.err.println(e);
85    }
86  }
87
88}