Wednesday, November 27, 2024

Java: Serializable

The following tutorial describes the Interface. Read on to discover its uses, why its important, and how to implement it.


java.io.Serializable is an important addition when saving your own personal objects.

The method reading in the object from a file must either handle the errors on its own or throw them to be caught elsewhere:
IOException: thrown upon a read error
and
ClassNotFoundException: thrown if the class has been modified since being written or an unknown object type is found

The method writing the file must either handle the error on its own or throw them to be caught elsewhere:
IOException: thrown upon a read error

*These errors are all subclasses of Exception, and if you are not concerned with specific errors, they can be grouped into a single catch block as I have done here.

Code Example:
import java.io.*;
import java.util.*; //Vector use
public class ObjectTest{
public void readObject(){
try{
ObjectInputStream in = new ObjectInputStream(new FileInputStream(“myFile3.dat”));
Vector v = (Vector)in.readObject();
in.close();
System.out.println(v);
}catch(Exception e){
System.out.println(“Error reading the specified file.”);
}
}
public void writeObject(Vector v){
try{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(“myFile3.dat”));
out.writeObject(v);
out.close();
}catch(IOException e){
System.out.println(“Error writting the specified file.”);
}
}

private static class Objects implements java.io.Serializable{
String text;
public Objects(String s){
text = s;
}
/*
public String toString(){
return text;
}*/
}

public static void main(String args[]){
ObjectTest ot = new ObjectTest();
Objects o = new Objects(“one”);
Objects o2 = new Objects(“Two”);
Objects o3 = new Objects(“three”);
//create a Vector of Objects, from my class Objects
Vector v = new Vector();
v.add(o); v.add(o2); v.add(o3);
ot.writeObject(v);
ot.readObject();
//output will be 3 memory addresses of the 3 objects created inside []
//remove the comments around the toString method, to show the Strings within these memory locations
//the brackets are added as a Java standard when outputing a Vector
}
}

Vectors are a common Object outputting as most often the Objects are illegable. In the case of Strings, they are still within the text of the file but mixed within the other text defining the object.

The ToString method is a special method for classes which I will discuss in my next tech-recipe.

*Also, try removing the implements java.io.Serializable. You will get both read and write errors as all objects must be serializable to be written in this manner.
*You should also take a look at the myFile3.dat file created in Notepad to get a better understanding of how Objects are saved.

Serializable is important because it is similar to applying a serial number to your object, and tells the JVM how your object is constructed. With this implemented, your structure is sure to be retained when reading the object back in.

Questions/Comments: [email protected]
-William. § (marvin_gohan)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!