473,388 Members | 1,340 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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

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
1 2828
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Pavan Arise | last post by:
Dear all.. I have a picturebox filled on a form. The picturebox has some graphics displayed on it.I was trying to save the picturebox, but continuesly failed to do so. I am clueless of why it is...
6
by: tracy | last post by:
I have a complex query (16 table join) that until five days ago took 30 sec to run. It now takes eight hours. I restored a backup of the database from five days ago and the query plans are...
7
by: Stan Sainte-Rose | last post by:
Hi, How can I save a page loaded with AxWebBrowser into a winform as an htm page ? Stan
1
by: garethrichardadams | last post by:
Hello all, I've got a user control I've created called canvas. Doesn't do much at the moment. I've set the background image to a small bitmap using BackgroundImage and I've set it to tiled...
6
by: Ryan | last post by:
I have a windows form that I want to force validation on controls (text boxes) when the user clicks a "Save" button. The only way I've found to do this is to cycle through every control and call...
1
by: teo | last post by:
I have a Web Form with a Button on it, nothing more. I load the page from VisualStudio2005 Asp Net 2.0 enviroment (with internal server) and immediately go to Save As... the page all is ok. ...
1
by: blintrell | last post by:
<?xml version="1.0" encoding="utf-8"?> <root> <groups> <group gid="1" name="CSM"> <contact cid="1" /> <contact cid="2" /> <contact cid="3" /> <contact cid="4" /> ...
7
by: Neil | last post by:
Was working in A2003 and noticed that the Save Record item on the Records menu was not available when the record was not dirty. In A2000, Save Record was always available. (This is a problem for me...
1
by: Rob Nicholson | last post by:
I've managed to write an ASP.NET v2 web page where the prompt to save the password doesn't appear :-( Not sure why it's not working. Autocomplete on the text boxes is working but when you enter...
2
by: =?Utf-8?B?QmlsbHkgWmhhbmc=?= | last post by:
I am using reporting service with asp.net. I want to save a report snapshot in some time. How do I do this in asp.net? Is there any web service to do this? Thanks in advance! -Billy
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.