This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Where Serialized Objects will be stored?

 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is my sample code,



All it will do is simply serialized and de-serialized. But, The serialized object, where will be stored? If it is jvm, then how can I store data permanently in serialized mode. Like for example, consider a simple game. Game have a feature like you can save the current level and you can shut down the game. When you open the same again(after so many days), it'll start from the level where you saved last time not from the beginning level. How can you achieve this. In that type of case, should we save that data into DB instead of doing serialize? I mean, serialization is volatile, like until and unless program shutdown serialization works after that it'll not right?(I think yes, exactly don't know)

Thanks:
Ramakrishna K.C
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The serialized objects are stored in the file "nameStore", like the code specifies. Where that file is created depends on what the default directory is when this code is run. If you want control over the directory, use absolute paths instead of relative paths.

serialization is volatile, like until and unless program shutdown serialization works after that it'll not right?

I'm not sure what you're trying to say here, but serialization is frequently used to store data for later use during a future invocation of the JVM. So I think your understanding is wrong.

Personally, I don't like to use binary serialization, I prefer XML serialization using the java.beans.XMLEncoder and XMLDecoder classes. For storing game status it should be easy to meet the requirement that the serialized classes obey JavaBeans conventions.

You should also check out the java.util.prefs package. For a desktop app that may be a better fit than serialization - for example, you don't have to worry about files, and where they might get stored.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The serialized object is stored wherever your code specifies it should be stored. In this case, that's controlled by the part that says

new FileOutputStream("nameStore")

That causes the code to create a new file (possibly overwriting an existing file) named "nameStore", in the current directory (the directory you're running from). Later code causes the program to read from that same file, to deserialize the object. You can read up on FileOutputStream, and input and output streams in general, to understand better how this works. A good place to start is the Java Tutorial on Basic I/O.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Curses - I've been Ulfed! ;)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic