ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
ok, im working on an assignment for class, the teacher could not answer my question so i will ask here.
the situation:
there are three files: WriteCountryInfo.java CountryInfo.java and ReadCountrynfo.java
the write class makes a series of countryinfo objects via a constructor in the countryinfo class then serializes them and creates a file. the readcountryinfo class de-serializes the data from the created file back into countryinfo objects.
in the book these programs use an array of countryinfo objects and writes them (and reads them) one at a time in a for loop.
the idea is to put the countryinfo objects in an ArrayList so that it can write all the objects to the file in one step instead of using the loop.
public class WriteCountryInfo
{
public static void main(String[] args) throws IOException
{
FileOutputStream file = new FileOutputStream("countries.dat");
ObjectOutputStream outStream= new ObjectOutputStream(file);
ArrayList countries = new ArrayList();
countries.add(new CountryInfo("United States of America","USA","Washington, D.C.", 9629091L, 278058900L));
countries.add(new CountryInfo("Russia","RUS","Moscow",17075200L,145470200L));
countries.add(new CountryInfo("Italy","ITA","Rome",301230L,57679800L));
countries.add(new CountryInfo("Sweden","SWE","Stockholm",449964L,8875100L));
countries.add(new CountryInfo("Poland","POL","Warsaw",312685L,38633900L));
int index=0;
while(index<countries.size())
{
like so. from here i can reference the individual indexes of the ArrayList no problem.
but when it reads the file it is currently pulling it up as one whole chunk without seperating back into CountryInfo objects.
public class ReadCountryInfo
{
public static void main(String[] args) throws Exception
{
FileInputStream file = new FileInputStream("countries.dat");
ObjectInputStream inStream=new ObjectInputStream(file);
ArrayList countries=new ArrayList();
countries.add(inStream.readObject());
int index=0;
while(index<countries.size())
{
I'm a long time non-OOPer, but I'm starting to learn a little these days. I'm just wondering (and this may be a bit of an aside, and certainly not helpful) why you'd want three separate classes like that? Shouldn't there just be a CountryInfo class which has operations for reading or writing its data, serially?
I just haven't seen classes named like operations (in my admittedly narrow OOP experience)...
Originally posted by rkef I'm a long time non-OOPer, but I'm starting to learn a little these days. I'm just wondering (and this may be a bit of an aside, and certainly not helpful) why you'd want three separate classes like that? Shouldn't there just be a CountryInfo class which has methods for reading or writing its data, serially?
i think that its just for ease of representation in the text book. show 1 prog to write and one to read and see how seperate classes can interact or something like that?
Try creating a new CountryInfo object and assigning the casted object back to it:
Code:
ArrayList countries=new ArrayList();
CountryInfo temp = (CountryInfo)inStream.readObject();
countries.add(temp);
int index=0;
or perhaps you need separate files for each of the serialised objects? I'm not sure how serialised objects work, though I do have quite a bit of Java experience if you do get stuck.
i think that its just for ease of representation in the text book. show 1 prog to write and one to read and see how seperate classes can interact or something like that?
Ah... fair enough; thanks for taking the time to answer my question . g/l.
ok i tried that and it didnt like the'(' coming right after the 'new' so i tried CountryInfo temp = new CountryInfo(inStream.readObject()); but it doesnt like that either.
i appreciate your help and if you come up with anything else then cool. im going to keep trying some stuff as well and if i get it ill post answer.
this is the part i didnt post, the constructor class:
//prog 8.9
//
import java.io.Serializable;
public class CountryInfo implements Serializable
{
private String name, abbreviation, capitol;
private long area, population;
public CountryInfo()
{}
public CountryInfo (String cName, String cAbbreviation, String cCapitol, long cArea, long cPopulation)
{
name=cName;
abbreviation=cAbbreviation;
capitol=cCapitol;
area=cArea;
population=cPopulation;
}
public String toString()
{
String result ="Name: "+name+"\n";
result +="Abbreviation:"+abbreviation+"\n";
result +="Capitol: "+capitol+"\n";
result +="Area: "+area+"\n";
result +="Population: "+population+"\n";
result +="^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"+"\n";
Originally posted by Brain Drop ok i tried that and it didnt like the'(' coming right after the 'new' so i tried CountryInfo temp = new CountryInfo(inStream.readObject()); but it doesnt like that either.
i appreciate your help and if you come up with anything else then cool. im going to keep trying some stuff as well and if i get it ill post answer.
Tried what? You're gonna have to quote which ever of us you're replying to.
so far your first suggestion of :
countries.add((CountryInfo)inStream.readObject());
seems to come the closest but i havent figured out yet how to avoid the cast exception. I'm going back to reading my text book for awhile, maybe the answer is hiding.
public class ReadCountryInfo
{
public static void main(String[] args) throws Exception
{
FileInputStream file = new FileInputStream("countries.dat");
ObjectInputStream inStream=new ObjectInputStream(file);
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.