Samooramad
asked on
not saving
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
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
ASKER
how do I need to change the code?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
just change the declaration to JavaBeans instead of class?
also, what do I need to import for your code objects?
also, what do I need to import for your code objects?
ASKER
if I change to java beans will they still work or will it affect other aspects of the program?
> also, what do I need to import for your code objects?
import java.io.*;
What are you using the serialized instance for?
import java.io.*;
What are you using the serialized instance for?
ASKER
I dont know what you mean
I got an error by the way.
not serializable object on one of the lines
I got an error by the way.
not serializable object on one of the lines
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
{
...
> 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
{
...
ASKER
oh ok :) will try that and see if I get any more trouble :)
thanks
thanks
ASKER
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
got any pointers?
thanks
Serialization should be fine then.
ASKER
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
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
How exactly is it not working?
ASKER
it saves the "name" attribute but doesnt seem to save the others..
ASKER
I really only need to save the two attributes "name" and the employee array. is there any way to just save those two?
yes, call the writeObject() method on each object you want to save:
out.writeObject(a);
out.writeObject(b);
out.writeObject(c);
out.writeObject(a);
out.writeObject(b);
out.writeObject(c);
ASKER
ok will try that
thanks
thanks
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.