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

From: hiwa (HGA03630_at_nifty.ne.jp)
Date: 01/31/04


Date: 30 Jan 2004 17:23:54 -0800

sungwoo.lim@brunel.ac.uk (maya2000) wrote in message news:<9bc66206.0401300322.46c0163c@posting.google.com>...
> 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();
> }
> }
> }
> //-------------------------------------------------------------------------
>From the Java Collection framework API design FAQ:

[quote]
# Why doesn't Collection extend Cloneable and Serializable?

Many Collection implementations (including all of the ones provided by
the JDK) will have a public clone method, but it would be mistake to
require it of all Collections. For example, what does it mean to clone
a Collection that's backed by a terabyte SQL database? Should the
method call cause the company to requisition a new disk farm? Similar
arguments hold for serializable.

If the client doesn't know the actual type of a Collection, it's much
more flexible and less error prone to have the client decide what type
of Collection is desired, create an empty Collection of this type, and
use the addAll method to copy the elements of the original collection
into the new one.
[/quote]
You could store/restore each element as Map.Entry object using
XMLEncoder and XMLDecoder.



Relevant Pages

  • Re: Socket problems
    ... I'm trying to write a 20 client chat server which is not ... >> public void listenFromClients() ... > // create a new communicator task and pass it the socket ...
    (comp.lang.java.programmer)
  • Re: Multiplayer gaming
    ... Server code: ... public void RunServer() { ... Client client = new Client); ... We have a very simple game with several spaceships which try to kill ...
    (microsoft.public.dotnet.csharp.general)
  • Remoting Error
    ... can the server application call methods of the shared Object?? ... public RemDeskBridge rdb; ... The procedure on client side is:- ... public void setUP ...
    (microsoft.public.dotnet.languages.csharp)
  • Client cannot locate Container-Managed Entity Bean Sun Applicaton Server Nine
    ... I cannot find my Course J2EE bean from a client. ... public void setName throws RemoteException; ... public void setNumber (int n) throws RemoteException; ...
    (comp.lang.java.programmer)
  • Deserialization exception when using MarshalByRef object with events
    ... I'm getting the following exception on the client when attempting to ... of code from the server, client, MarshalByRef object, and .config ... public void Start ... public event BombHandler BombDetonated; ...
    (microsoft.public.dotnet.framework.remoting)