473,651 Members | 2,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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("_str okeNumber", new Integer(strokeN umber));
siMap.put("_Vec tor1", Vector1);
siMap.put("_Arr ay2", Array2);
}

public void saveData(String filename) {
FileOutputStrea m fos = null;
try {
fos = new FileOutputStrea m(filename);
ObjectOutputStr eam oos = new ObjectOutputStr eam(fos);
oos.writeObject (siMap);
oos.flush();
fos.close();
} catch(IOExcepti on e) {
e.printStackTra ce();
}
}

public void loadData(String filename) {
FileInputStream fis = null;
try {
fis = new FileInputStream (filename);
ObjectInputStre am ois = new ObjectInputStre am(fis);
newMap = (Map)ois.readOb ject();
fis.close();
} catch(IOExcepti on e) {
e.printStackTra ce();
}
}
}
//-------------------------------------------------------------------------
Jul 17 '05 #1
1 2840
su*********@bru nel.ac.uk (maya2000) wrote in message news:<9b******* *************** ****@posting.go ogle.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("_str okeNumber", new Integer(strokeN umber));
siMap.put("_Vec tor1", Vector1);
siMap.put("_Arr ay2", Array2);
}

public void saveData(String filename) {
FileOutputStrea m fos = null;
try {
fos = new FileOutputStrea m(filename);
ObjectOutputStr eam oos = new ObjectOutputStr eam(fos);
oos.writeObject (siMap);
oos.flush();
fos.close();
} catch(IOExcepti on e) {
e.printStackTra ce();
}
}

public void loadData(String filename) {
FileInputStream fis = null;
try {
fis = new FileInputStream (filename);
ObjectInputStre am ois = new ObjectInputStre am(fis);
newMap = (Map)ois.readOb ject();
fis.close();
} catch(IOExcepti on e) {
e.printStackTra ce();
}
}
}
//-------------------------------------------------------------------------

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
31510
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 happening. I tried to save the image of the form & also the picturebox, but always, i see a blank file saved. Is there a way to save whatever is displayed on the picturebox, to a file?? Thanks for any suggestions and help. Regards,
6
2100
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 completely different for the same exact query. Five days ago the final estimated row count is 1.6M now it is 1.7E+10 OUCH! The amount of data added in those five days is insignificant (percentage wise). I rebuilt all the indexes using DBCC DBREINDEX...
7
3803
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
6848
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 using BackgroundImageLayout = ImageLayout.Tiled. It all works - I get a tiled image.
6
4744
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 it's .Select() method. This is clunky though because you can see a flash in each text box as it's being validated. Here's my code Private Sub Save() For each c as control in Me.Controls If c.CanSelect() then c.Select()
1
1152
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. Again, I load the page from VisualStudio2005 Asp Net 2.0 enviroment
1
1703
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" /> </group> <group gid="2" name="RISIK">
7
2165
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 because I have Docmd.Runcmd acCmdSaveRecord code all over the place, which never gave an error before. But now it was giving an error when the Save Record menu command wasn't available.) So I went back to A2000 and confirmed that the Save...
1
2269
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 username, password (text box with type = password) and click the login link button, IE doesn't popup "Do you want to save this password?". The setting is turned on in IE7 as I've just cleared all my forms and passwords and gone to a couple of web...
2
2110
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
8361
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8278
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8807
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8584
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7299
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5615
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.