Use ObjectOutputStream and ObjectInputStream to write and read Hashtable : ObjectInputStream « File Input Output « Java






Use ObjectOutputStream and ObjectInputStream to write and read Hashtable

  

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.Hashtable;

public class Main {
  public static void main(String[] args) throws Exception {
    Hashtable h = new Hashtable();
    h.put("string", "AAA");
    h.put("int", new Integer(26));
    h.put("double", new Double(Math.PI));

    FileOutputStream fileOut = new FileOutputStream("hashtable.ser");
    ObjectOutputStream out = new ObjectOutputStream(fileOut);
    out.writeObject(h);
      
    FileInputStream fileIn = new FileInputStream("h.ser");
    ObjectInputStream in = new ObjectInputStream(fileIn);
    Hashtable h = (Hashtable)in.readObject(  );
    System.out.println(h.toString(  ));      
  }
}

   
    
  








Related examples in the same category

1.Create ObjectInputStream out of FileInputStreamCreate ObjectInputStream out of FileInputStream
2.new ObjectInputStream(new BufferedInputStream(new FileInputStream(StringFileName)));
3.Reading objects from file using ObjectInputStream
4.Read different data types from ObjectInputStream
5.Object serialization with Serializable interface, ObjectOutputStream and ObjectInputStream.
6.ObjectInputStream, ObjectOutputStream and java.io.Serializable
7.Store objects in file
8.ObjectStream Test with hierarchy