• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I/O stream to List class????? argh!!

 
Paul Batterbury
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi was wondering if you could help me I am really stuck!

I got some course work from university, basically I have to write an application which uses RMI, now I have that part of cracked, and second part is I need to save what is stored in memory, I have that part also done, see below:

oos.writeObject(list);

now when I quit the application from the server side, everything in the RMI registry should be saved in the external text file, which it is! when I execute the application the application then reads the txt file and restore the list into the rmi registry..... this is where the problem starts. I need to read the list back:

List list = (list) ois.readObject();


I need to write a new class List, but I do not know how to this I tried using an array, my java is very week and I really need to sort this out, as I have to repeat the methods another two times as I need to read in from three different files which all are list.

Please, please, can you help me out with sample code for the List class so that I can adapt it.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Batterbury:
List list = (list) ois.readObject();

I need to write a new class List, but I do not know how to this I tried using an array
First the cast is incorrect; you need to put the class type inside the parentheses -- not the variable name.I'm confused. Why can't you use the List you're reading from the file? Since writing to the file works, I can only assume that you are using some List implementation already. Why can't you continue to use that?

Or is the assignment to write your own List implementation? If that's the case, you can read the ArrayList and LinkedList classes to get some ideas. The source code for all the Java classes is in the src.zip archive installed with your JDK.
 
Paul Batterbury
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok,

say the List is a Menu, and therefore I have a class for this. When I add dishs to the menu and want to save the complete menu i use:

oos.writeObject(list);

this write out everthing store in the RMI list to the external file, on reading in the data i need to read the list in, due to it being a list when writen so my code would look like this:


Menu menu = (Menu) ois.readObject();


How do I write the class Menu so it is compatable with the list writen out?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main thing to realize is that you will read in the exact same object that you wrote out. If you wrote out a String and then tried to read it in as a StringBuffer, you'd get a ClassCastException. I think you're aware of this, but I'm just making sure we're on the same page.

The short of it is this: if you want to be able to read in a Menu, you must write out a Menu.

If a Menu is a List of Dish objects, you could extend one of the List classes , but I don't recommend this as Menu is probably a bit more complex than that. For one thing, Menus tend to change over time, and you may want to capture the dates as part of the Menu itself. In that case, you'd have a List as a member of class Menu (called composition).Of course, you probably want to group like Dishes together: soups & salads, appetizers, main courses, etc. Can you describe a little more detail of what you want to do here?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic