Save object to file:
FileOutputStream fileStream = new FileOutputStream("output.out");
ObjectOutputStream os = new ObjectOutputStream(fileStream);
os.writeObject(serilaziableObject);
os.close();
Low-level Represent connections to destinations and sources such as files or network sockets.
Chain stream:
Work only if chained to other streams. Can be called method on.
During serialization, the object's instance variables are saved.
Serialization saves the entire object graph. All objects referenced by instance variables are also serialized.
Serialize interface is known as a marker or tag interface, which doesn't have any methods to implement. It is an announcement that it is saveable through the serialization mechanism.
If any superclass of a class is serializable, the subclass is automatically serializable.
Serialization is all or nothing. The entire object graph should be serialized correctly or fail.
Mark an instance variable as transient if it can't (shouldn't) be saved. Transient says don't save this variable during serialization, just skip it.
Static variables are not saved in file. Their values are the current class values after the class is first loaded in JVM.
Don't make serializable objects dependent on a dynamicallly-changing static variable! It might not be the same when the object comes back.
You can't save network connections, thread, or file objects. They are all depend on (and specific to) a particular runtime 'experience'. They are instantiated in a way that's unique to a particular run of your program, on a particular platform, in a particular JVM.
Class is not saved. Classnotfound.
Objects are saved in order.
Deserialization:
FileInputStream fileStream = new FileInputStream("input.in");
ObjectInputStream os = new ObjectInputStream(fileStream);
Object one = os.readObject();
Box box = (Box)one;
os.close();
During deserialization, the class of all objects in the graph must be available to the JVM. The JVM must have these classes.
Instance variables are given the values from the serialized state. Transient variables are given default values.Objects are deserialized in the same order as they are saved.
File:
1. Make a file object.
File f = new File("code.txt");
2. Make a new directory.
File dir = new FIle("Dir");
dir.mkdir();
3. List the content of a directory
if(dir.isDirectory()) {
String[] dirContents = dir.list();
}
4. Absolute path of a file/directory
dir.getAbsolutePath();
5. Delete
boolean isDeleted = f.delete();
Write to a file:
1. Without buffer
FileWriter writer = new FileWriter("fo.txt");
writer.write("hellow!");
writer.close();
2. With buffer
BufferedWriter writer = new BufferedWriter(new FileWriter("fo.txt"));
writer.write("hello!");
writer.close();
First method writes to the file every time you call write.
Second method writes the the file when the buffer is full or you call flush().
Read from a file:
BufferedReader reader = new BufferedReader(new FileReader("fo.txt"));
String line = null;
while((line = reader.readLine()) != null) {
// line
}
reader.close();
serialVersionUID:
The serialized object should be able to deserialized and is compatible with its class version in current JVM.
The compiler checks the compatibility based on the serialVersionUID.
You take responsibility for making sure that the serialized object is compatible with the class declared in current JVM.
Otherwise Deserialization exception?