By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 883 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

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

P: n/a
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();
}
}
}
//-------------------------------------------------------------------------
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
su*********@brunel.ac.uk (maya2000) wrote in message news:<9b**************************@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:

# 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.
You could store/restore each element as Map.Entry object using
XMLEncoder and XMLDecoder.
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.