JAVA Beginners part 17

How to save data in a Java program.

a) use serialization if the programs that use the saved data are Java

b) write a plain text file

Steps to save an object through serialization.

FileOutputStream filestream = new FileOutputStream(“mygame.ser”);
this will create a file

ObjectOutputStream os = new ObjectOutputStream(filestream);
this lets you to create objects, but it cannot directly write to a file which is why it needs a helper.

os.writeObject(one);
os.writeObject(two);
os.writeObject(three);

os.close();

closing the stream, file stream will also close automatically.

fileoutputstream writes bytes to file
objectoutputstream turns objects into data that can be written

Having two different stream gives flexibility.

test4

simple steps
————
foo myfoo = new foo();
myfoo.setwidth(37);
myfoo.setheight(70);

fileoutputstream fs = new fileoutputstream(“foo.ser”);
objectoutputstream oj = new objectoutputstream(fs);

os.writeobject(myfoo);

when an object is serialized, all the objects it refers to from instance variables are also
serialized . and all the objects those objects refer to are serialized and the best part is , it happens automatically.

serialization saves the entire object graph.

If a super class is serializable then the sub class is automatically serializable even if the subclass does not explicitly implement the interface.

java import.io.*;

When you say implements Serializable there is no method that you need to implement , it is a way to say to the JVM that it is ok to serialize the object of this type. Note that sub class need not be serializable but the classes that are being referenced
in one class needs to be.

If you want an instance variable to be skipped by the serialization process, mark the variable with the transient keyword.

Transient says that dont save this variable during Serialization just skip it. Run time variables are normally the ones that cannot be serialized.

import java.net.*;
class Chat implements Serializable {
transient String currentID;

Transient reference instance variable will be brought back as null, regardless of the value it had at the time it was saved during de serialization.

How to de Serialize

fileinputstream filestream = new fileinputstream(“foo.ser”);
objectoutputstream os = objectoutputstream(filestream);

Object one = os.readObject();
Object two = os.readObject();
Object three = os.readObject();

readObject will read objects one after the other.trying to read more objects will lead to an error.

gamecharacter elf = (gamecharacter) one;
gamecharacter troll = (gamecharacter) two;
gamecharacter magic = (gamecharacter) three;

os.close();

What happens when an Object is de Serialized ?

A new object is given space on the heap, but the serialized object constructor does NOT run obviously, if the constructor ran, it would restore the state of the object back to its original ‘new’ state, and that’s not what we want. We want the object to be restored to the state it had when it was serialized, not when it was first created.

If the object has a non-serializable class somewhere up its inheritance tree, the constructor for that non-serializable class will run along with any constructors above that (even if they’re serializable). Once the constructor chaining begins, you can’t stop it,

which means all super classes, beginning with the first non-serializable one, will reinitialize their state.

The moral : don’t make serializable objects dependent on a dynamically-changing static variable It might not be the same when the object comes back.

Static variables are not serialized It doesn’t make sense to save static variable value as part of specific objects state. since all objects of that type share only a single value the one In the class.

To write a String into a normal text file.

t2

Features of a File Object
– Make a new directory
– list the contents of the directory
– get the absolute path of the file or directory
– delete a file or directory

file object does not give access to the data in the file.

The cool thing about buffers is that they’re much. more efficient than
working without them. You can write to a file using FileWriter alone, by
calling write (some String) , but FileWriter writes each and every thing you
pass to the file each and every time. That’s overhead you don’t want or
need. since every trip to the disk is a Big Deal compared to manipulating
data in memory. By chaining a BufferedWriter onto a FileWriter, the
BufferedWriter will hold all the stuff you write to it until it’s full. 

BufferedWriter writer =new BufferedWriter(new FileWriter(aFile));

This is very useful, Reading from a File

simple.jpg

test

test3

File object is more of a File pointer.

You cannot teach a Old Dog new Code.

Things that can hurt the DE serialization.
– changes to the class like below
– changing the declared type
– moving the class up and down the hierarchy
– changing the class from serializable to not serializable.

Each time and object is serialized, the object and every object in its graph
gets stamped with a version ID. If an object is changed since the time of
DE serialization then the serialization will fail.

If an Object is serialized with say v 23, then the JVM will compare that with
compare that with the latest version of the object. If they are different it will fail.

The solution is to put a serialVersionUID
The class is compatible with this serialized object. even though the class has actually changed.
This works only if you’re careful with your class changes.

To get the serialVersionID
use serialver Dog

test2

test4.jpg

m1m3


 

next page 490

More in next part.

References:

Head First Java 2nd Edition

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s