Solved

not saving

Posted on 2004-09-21
17
180 Views
Last Modified: 2010-03-31
Hi experts,
I got some help with a question last week were I was trying to save my 2 arrays of classes(doctors and hospitals)

both these classes have string, boolean and integer (and even a class) attributes.

the code used saves the array and a string attribute"name" but it doesnt seem to save the boolean or class attribues

here is part of the code. if you have any solutions or alternatives please include code

    try  {XMLEncoder r = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(fileName)));}

    catch (Exception x)    {x.printStackTrace();}

    r.writeObject(allDoctors);
    r.writeObject(allHospitals);
    r.close();
  }

thank you
0
Comment
Question by:Samooramad
  • 10
  • 7
17 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12119220
XMLEncoder is limitied in what it can do, depending on how your class is written.
It is designed to work on beans.

If you just want to serialize your class to be read by another Java application then ObjectOutputStream is perhaps a better approach.
0
 

Author Comment

by:Samooramad
ID: 12121425
how do I need to change the code?
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 12121456
either change to use ObjectOutputStream:

 try  {ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(fileName)));  
    out.writeObject(allDoctors);
    out.writeObject(allHospitals);
    out.close();
}

    catch (Exception x)    {x.printStackTrace();}

  }


Or modify the classes you are serializing to be JavaBeans suitable for serializing with XMLSerializer
0
 

Author Comment

by:Samooramad
ID: 12127920
just change the declaration to JavaBeans instead of class?

also, what do I need to import for your code objects?
0
 

Author Comment

by:Samooramad
ID: 12127931
if I change to java beans will they still work or will it affect other aspects of the program?
0
 
LVL 92

Expert Comment

by:objects
ID: 12128789
> also, what do I need to import for your code objects?

import java.io.*;

What are you using the serialized instance for?
0
 

Author Comment

by:Samooramad
ID: 12130700
I dont know what you mean
I got an error by the way.
not serializable object on one of the lines
0
 
LVL 92

Expert Comment

by:objects
ID: 12130762
I meant why do you need to save you object(s) to a file.

> I got an error by the way.
> not serializable object on one of the lines

Sorry forgot to add you'll need to make your class implement Serializable

eg.

public class Doctor implements java.io.Serializable
{
   ...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Samooramad
ID: 12149982
oh ok :) will try that and see if I get any more trouble :)

thanks
0
 

Author Comment

by:Samooramad
ID: 12149991
to answer your question I have an array of class Doctor and an array of class Hospital, some calculations are done then some doctors are set as employees of certain hospitals (obviously more than one doctor ends up in each hospital). I just need to save the final result (meaning all hospital names and all the employees in them)

got any pointers?

thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 12153122
Serialization should be fine then.
0
 

Author Comment

by:Samooramad
ID: 12154453
I dont get errors but it still isnt working..
say I only want to save certain fields in each element of the array ..like in the hospital class, I only want to save the name attribute(string) and the employee attribute(array of String)
how do I go about doing that?
and how do I read them?

thanks for the help
0
 
LVL 92

Expert Comment

by:objects
ID: 12165302
How exactly is it not working?
0
 

Author Comment

by:Samooramad
ID: 12181630
it saves the "name" attribute but doesnt seem to save the others..
0
 

Author Comment

by:Samooramad
ID: 12183800
I really only need to save the two attributes "name" and the employee array. is there any way to just save those two?
0
 
LVL 92

Expert Comment

by:objects
ID: 12185059
yes, call the writeObject() method on each object you want to save:

out.writeObject(a);
out.writeObject(b);
out.writeObject(c);
0
 

Author Comment

by:Samooramad
ID: 12188611
ok will try that

thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
network + 7 81
Error with Java/Cache JDBC Classpath 2 31
eclipse formatting 6 74
diffSum example 4 27
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now