[Q] How to save a Map, which has non-primitive data, in a file

From: maya2000 (sungwoo.lim_at_brunel.ac.uk)
Date: 01/30/04


Date: 30 Jan 2004 03:22:43 -0800

Hello,
Below code has a problem to read a data. It seems the non-primitive
data are the reason. How can I save this Map into a file, and read it
later?
Thanks in advance.

//-------------------------------------------------------------------------
import java.io.*;
import java.util.*;

public class StoreData implements Serializable {
    public Map siMap = new HashMap();
    public Map newMap = null;
        
    public StoreData(int strokeNumber, Vector Vector1, Array array2) {
        siMap.put("_strokeNumber", new Integer(strokeNumber));
        siMap.put("_Vector1", Vector1);
        siMap.put("_Array2", Array2);
    }

    public void saveData(String filename) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(filename);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(siMap);
            oos.flush();
            fos.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
    
    public void loadData(String filename) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(filename);
            ObjectInputStream ois = new ObjectInputStream(fis);
            newMap = (Map)ois.readObject();
            fis.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}
//-------------------------------------------------------------------------



Relevant Pages

  • Re: Meyerss preference for vectors over maps
    ... >contiguity of vector elements in memory ensure ... I will a container of objects that are much ... >Reason is what I'm trying to understand better. ... >of vector vs. map very unclear. ...
    (comp.lang.cpp)
  • Re: Good Dungeon Mapping e-Tool?
    ... Kyle Wilson wrote: ... elements to create a map is that they change sizes as you zoom, ... Lots of very cool open source projects have died because ... a reason to pick one particular interesting off-side ...
    (rec.games.frp.dnd)
  • Re: The Vinland Maps Ink
    ... > think that's one reason some folks, including Seaver, considered ... that the forger's (assuming for argument's sake that it is a forgery) ... OR he/she who was forced to make the map, ... The germans did not know writing or latin ...
    (sci.archaeology)
  • Re: Perl on the Pocket PC - Easy!
    ... thing to a size proper for the Pocket PC? ... Almost never any reason to initialize an array to. ... map() should almost never be used in a void context. ... Knowing how to use them is a good step in the right direction. ...
    (comp.lang.perl.misc)
  • Meyerss preference for vectors over maps
    ... I will a container of objects that are much ... Reason is what I'm trying to understand better. ... of vector vs. map very unclear. ... is good for binary search in particular. ...
    (comp.lang.cpp)