flyinghigh
flyinghigh

Reputation: 235

How to use ObjectOutputStream and ObjectInputStream

Details:

My program is a grade book with 5 classes that are all aggregated. GradeBook has courses, Course has Categories, Category has Grades (all ArrayLists). My program also has a StateManager whose sole purpose is to return references to Objects because of the deep aggregation. In my Driver I do not create an instance of a GradeBook but a statemanager which has a static instance of a GradeBook with methods to return references.

My goal is to save all of this data to be reopened when the program is rerun.

Questions:

When I write the file all I need to do is write the StateManager object, correct? I think I've even accomplished this. I have the program create a "gradebook.data" file. Is there a way to open the .data file in a text program and see if it is writing correctly?

Where do I open the object again with inputstream? In the static main method or in the beginning of my method that initializes all of the graphics?

Thanks

Upvotes: 1

Views: 925

Answers (3)

Marko Topolnik
Marko Topolnik

Reputation: 200138

Serializing StateManager won't do anything because you have a static reference to the GradeBook. This is in itself a code smell, but here it has the physical repercussion of not getting serialized -- only instance fields get serialized. So remove the static qualifier. You can make the StateManager itself a singleton and have a static reference to it.

However, I am still in doubt as to why you don't serialize the GradeBook instance. That would be a far more logical approach. We don't usually serialize service objects, but data objects, and you already have that separation.

Upvotes: 1

mprabhat
mprabhat

Reputation: 20323

No, no text editor will show you all the details as a correct text format as what you have written is bytes and your text editor wants you to provide text.

Loading objects from disk to memory - two approaches

  1. Load them before hand, in the sense load them in main method, in case you dont use them your effort of loading them from disk went in vain, you used memory and CPU but didnt use it further.

  2. Load them when you access them the first time called as lazy loading, so when you access the static method of your StateManager, if your object is null you will load them from disk.

Offtopic:

A nice article which explains Serialization in detail.

Upvotes: 0

Neil Coffey
Neil Coffey

Reputation: 21795

You need to read the data in somewhere "early on" in your program before it is first used. In the initialisation code of your StateManager could be one place.

I can't see why the "method that initialises all of the graphics" would be a logical place.

Upvotes: 0

Related Questions