20-CS-694 Advanced Programming Techniques Spring 2012
File Classes

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

     Previous      Next     Sample file      Applet Source      All lectures
          Serializable Interface    ObjectInputStream Class    ObjectOutputStream Class

Serialization - Store/Retrieve Objects

Sample file

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.lang.reflect.*;

class FileOps4 {
   File4 f4;
   
   FileOps4 (File4 f4) {
      this.f4 = f4;
   }

   // The test file is expected to have two objects: the first is a hash
   // table, the second is a vector with the hash table as element 3.
   // The code below finds the second object, invokes the "elementAt"
   // method to get the hashtable.  Then invokes the "get" method on the
   // hashtable to get the hashtable elements.
   void readFromFile (String filename) {
      Object h, o;
      Class  c;
      Method m;

      try {
         FileInputStream fis = new FileInputStream (filename);
         ObjectInputStream ois = new ObjectInputStream(fis);
         h = ois.readObject();
         h = ois.readObject();
         c = h.getClass();                   // Should be a Vector class
         m = c.getMethod("elementAt",        // elementAt(int) method
                         new Class[] { Integer.TYPE });
         o = m.invoke(h, new Object[] { new Integer(0) });      // element 0
         f4.text.setText("Vector: < "+o.toString()+", ");
         o = m.invoke(h, new Object[] { new Integer(1) });      // element 1
         f4.text.append(o.toString()+", ");
         o = m.invoke(h, new Object[] { new Integer(2) });      // element 2
         f4.text.append(o.toString()+", ");
         o = m.invoke(h, new Object[] { new Integer(3) });      // element 3
         c = o.getClass();                   // Should be a Hashtable class
         m = c.getMethod("get",              // get(Object) method
                         new Class[] { (new Object()).getClass()});
         h = m.invoke(o, new Object[] { new String("one") });   // key "one"
         f4.text.append(" {"+h.toString()+", ");
         h = m.invoke(o, new Object[] { new String("two") });   // key "two"
         f4.text.append(h.toString()+", ");
         h = m.invoke(o, new Object[] { new String("three") }); // key "three"
         f4.text.append(h.toString()+"} >\n");

         fis.close();
         f4.messages.setText("");
      } catch (StreamCorruptedException e3) {
         f4.messages.setText("Cannot read object from (corrupted?) file");
      } catch (IOException e) {
         f4.messages.setText("File "+filename+" Not Found");
      } catch (Exception ee) {
         f4.messages.setText("Error: "+ee);
      }
   }

   public void writeToFile(String filename) {
      Hashtable  h = new Hashtable  ();
      h.put("one", new String("Hello There"));
      h.put("two", new Integer(23));
      h.put("three", new Double(1.23));
      Vector  v = new Vector  ();
      v.addElement(new String("Goodbye"));
      v.addElement(new Integer(43));
      v.addElement(new Double(4.34));
      v.addElement(h);
      try {
         FileOutputStream fos = new FileOutputStream (filename);
         ObjectOutputStream out = new ObjectOutputStream(fos);
         out.writeObject(h);
         out.writeObject(v);
         fos.close();
      } catch (IOException e) {
         f4.messages.setText("Could not write to "+filename);
      }
   }
}

public class File4 extends Applet implements ActionListener {
   JTextArea text;
   JTextField filename, messages, delimiter, number;
   JButton read, write;
   FileOps4 fo;
   
   public void init () {
      setLayout(new BorderLayout());
      JPanel p = new JPanel();
      p.setLayout(new GridLayout(2,1));
      JPanel q = new JPanel();
      q.setLayout(new BorderLayout());
      JPanel r = new JPanel();
      r.setLayout(new GridLayout(2,1));
      r.add(filename = new JTextField());
      r.add(new JLabel("Filename", JLabel.CENTER));
      q.add("Center", r);
      r = new JPanel();
      r.setLayout(new GridLayout(2,1));
      r.add(new JLabel(""));
      r.add(new JLabel(""));
      q.add("East",r);
      p.add(q);
      q = new JPanel();
      q.setLayout(new BorderLayout());
      r = new JPanel();
      r.setLayout(new GridLayout(2,1));
      r.add(messages = new JTextField());
      r.add(new JLabel("Messages", JLabel.CENTER));
      q.add("Center", r);
      r = new JPanel();
      r.setLayout(new GridLayout(2,1));
      r.add(new JLabel(""));
      r.add(new JLabel(""));
      q.add("East", r);
      p.add(q);
      add ("North", p);
      add ("Center", new JScrollPane(text = new JTextArea(30,30)));
      
      p = new JPanel();
      p.setLayout(new GridLayout(1,3));
      p.add(read = new JButton("Read"));
      p.add(new JLabel());
      p.add(write = new JButton("Write"));
      add ("South", p);
   }
   
   public void start () {
      fo = new FileOps4(this);
      read.addActionListener(this);
      write.addActionListener(this);
   }
   
   public void actionPerformed (ActionEvent evt) {
      if (evt.getSource() == read)
         fo.readFromFile(filename.getText());
      else if (evt.getSource() == write)
         fo.writeToFile(filename.getText());
   }
}