Introduction At the time of development, it is sometimes necessary to save the state of the object in the file system. Some objects may or may not be stored in the file system depends on the intensity of the structural object graph. In this article I will focus on two important aspects of object persistence. Before going on the subject, I would like to tell you about the importance of object persistence. Object persistence is the state of the object in the file system. In this issue you can do the arguments in relation to object persistence in the database, Hibernate. But so far this article is concerned I will give insight on the persistence in the file system for all convenience. The state of the object means the attributes or characteristics of the object in a broader sense. The object diagram shows the internal morphological structure of the object. More incessantly the object is, you will save all changes in internal structure of the object. Technicalities There are several ways you can pass the condition of the property. You can help Java IO system to store the object in the file system. However, there are practical approaches that are your expectations in this regard needs. One possibility is the textual representation of the object graph in the file system and the other type is the binary representation of the object graph. These ways are very convenient and easy from the perspective of development. You can reach the textual representation of the object graph with XMLEncoder and you can get the binary representation of the object graph with Java serialization goal. Let me explain by the two approaches. Persistence with XMLEncoder XMLEncoder class perpetuate an approach to the object graph in an XML document or simply in an XML file. It offers the flexibility of storing the object as a textual approach. In this approach, you can use the XML file and one can easily understand the attributes of the object. As for the object graph from the XML file, you can XMLDecoder. All these classes have been defined in the Java. Beans package. Let me clarify all aspects by specifying the full sample. Create a standard Java bean class or with the following structure. Let the class called Emp. Java, which is a normal Java Bean. There is another class called TestPersistence test environment. Java, which exposes the use of XMLEncoder and XMLDecoder. The following is the receiver. Java. com package. Core. ; Are / ** * This is a simple Java Bean. * @ Author Debadatta Mishra (PIKU) * * / public class Emp ( private String name = null; private int age = 0; private String EmpID = null; Public (EMP) ( super (); ) public String getName () ( Return name; ) public void setName (String name) ( this. name = name; ) public int getAge () ( Return age; ) public void Set (int age) ( this. age = age; ) public String getEmpId () ( EmpID return; ) public void setEmpId (String EmpID) ( this. EmpID = EmpID; ) ) What follows is the TestPersistence. Java com package. Core. ; Are import java. Beans. XMLDecoder; import java. Beans. XMLEncoder; import java. io. BufferedInputStream; import java. io. BufferedOutputStream; import java. io. FileInputStream; import java. io. FileOutputStream; / ** * This is a test environment to the class * Use of XMLEncoder and XMLDecoder. * @ Author Debadatta Mishra (PIKU) * * / public class TestPersistence ( public static void main (String [] args) ( EMP Emp = new EMP (); EMP. setName (“John”); EMP. SetAge (23); EMP. setEmpId (“A123″); try ( / * * The following codes are used to the Emp object graph are * / XMLEncoder encoder = new XMLEncoder (new BufferedOutputStream ( new FileOutputStream (“C: / EMP. xml “))); Encoder. writeObject (EMP); Encoder. flush (); Encoder. close (); / * * The following codes are used to obtain the Emp object graph * From the XML document * / XMLDecoder decoder = new XMLDecoder (new BufferedInputStream ( new FileInputStream (“C: / EMP. xml “))); Emp EMP1 = (EMP) decoder. readObject (); Decoder. close (); System. out. (Println “EMP name = >” + EMP1. GetName ()); System. out. Println (“EMP age = >” + EMP1. GetAge ()); System. out. Println (“EMP > Id =” + EMP1. GetEmpId ()); ) catch (Exception e) ( e. printStackTrace (); ) ) ) The following is the output of the above example. If the above classes, an XML document named EMP. XML will be created in the specified location. The XML document will look like. 23 A123 John So you have saved the state of the receiver object in the XML document. It is also necessary to load the receiver object from the XML document. For this purpose, you must use XMLDecoder that was used in the test environment class. If you want to test the above two classes, you can copy and modify the class and package structure and you can run it. When loading the object with XMLDecoder, it takes the help of Java’s reflection system. Advantages of XMLEncoder and XMLDecoder • Since this is a textual representation of the object graph, all can see, the XML file, and it helps in the portability to other systems. • If you want to change the value of a particular property of an object, you can jump in the XML document, so that during use XMLDecoder, you will get your modified value. • If the object variables are declared transient, nor can you save the entire object graph along with the transient variable value. This case is not possible in the case of Java object serialization. • It is also very easy and convenient in the case of inheritance object. Is there no need to disturb the super class and sub class. may cause some of the limitations of the normal Java object serialization on this approach. Persistence with serialization Serialization is a Java standard mechanism to store the state of the object or simply the object graph in the file system. In this case, your object in the file system where the file will be maintained unreadable. It means that you’re going to save the binary representation of the object graph in the file system. This object can be serialized with the writeObject () method to the class ObjectOutputStream. The main thing you have to remember is that the object you continue to have to go serialization interface, which is called as a marker interface implemented. In the next article I will explain the use and beauty of the marker interface. Similarly, deserialization retrieval of funds from the object saved state. You can use the deserialization readObject () method of class Object Input Stream. Please note the following piece of code to achieve serialization. The following class named Emp. It implements Serializable interface. There is another class called TestSerialization. This class leads both serilization and deserilization. This is the normal way of serilization concept of Java. com package. Core. ; Are import java. io. Serializable; / ** * This is a simple Java Bean. * @ Author Debadatta Mishra (PIKU) * * / public class Emp implements Serializable ( private static final long serialVersionUID =-164971138528601769L; private String name = null; private int age = 0; Public (EMP) ( super (); ) public String getName () ( Return name; ) public void setName (String name) ( this. name = name; ) public int getAge () ( Return age; ) public void Set (int age) ( this. age = age; ) ) TestSerialization class. Java com package. Core. ; Are import java. io. FileInputStream; import java. io. FileOutputStream; import java. io. Object Input Stream; import java. io. ObjectOutputStream; public class TestSerialization ( public static void main (String [] args) ( EMP Emp = new EMP (); EMP. SetAge (23); EMP. setName (“John”); try ( / * * Code to serialize the object to stop or * / ObjectOutputStream OU = new ObjectOutputStream (new FileOutputStream (“D: / test. SER”)); Organizational units. writeObject (EMP); Organizational units. close (); / * * Code to deserialize the object * / Object Input Stream oin = new Object Input Stream (new FileInputStream (“D: / test. SER”)); Emp EMP1 = (oin EMP). readObject (); System. out. Println (“EMP age —-” + EMP1. GetAge ()); System. out. Println (“EMP name —-” + EMP1. GetName ()); ) catch (Exception e) ( e. printStackTrace (); ) ) ) You can test the above code in your editor to the functions relating to the serialization. Now I have put forward some cases for seriliazation. Case 1: If you are not your object implements Serializable interface, To serialize an object, it is necessary that the class must implement seriliazable interface. This is the principle of serilization required. Oterwise throw it NotSerializationException. There is another way, if your class does not implement Serilizable interface, you must declare the object as transient. So that the object state will not be retained. Case 2: In the case of inheritance, your super class does not implement Serializable interface and sub-class either. serilization In this case, will not happen. If you are interested in the properties of the object store, all you are traveling for XMLEncoder XMLDecoder and as I explained to you alredy. Case-3: In the case of inheritance, your class implements Serializable interface and sub-class does not. In this case, you should not worry about it, seriliazation happened. Case-4: In the case of inheritance, the super-class does not implement Serializable interface, but does your sub-class. Seriliazation will happen, but falvour with a lemon. No exception will be thrown, but your data superclass members or object properties of your super-class will not be retained. When you deserialize object, you get the defaults from your super-class object. Case-5: This is the best case. You implement super class and sub class Serializable interface. Everything here is ok, happens serilization. Case 6: If your object uses transient modifier in the interior of the object, You have to remember that transient objects or variables are not preserved during serialization. Case 7: If your object volatile modifiers used within the object, There is nothing to fear, the serialization and data happen to be retained in the rule. Case-8: If your object uses static modifiers inside the object, You have to remember that, since not static part of the object, the static variable or static object reference will not be retained for seriaization. Case 9: It is a very special case, I will focus on me. You can use the following situations at the time of serialization encounter. • you are not sure if the super-class implement Serializable interface works. • You do not have access to the source code of your super class. • your super class can be a final class. • can contain the super-class noe-serializable object reference. In this case, if you can feel the frustration and disappointment, you XMLEncoder XMLDecoder and go as I have already explained. If you want to hold the object in the Java serialization mechanism and approach, you need to do it manually, and unintelligent. Please refer to the following piece of code. The following class name is Emp. Java com package. Core. ; Are import java. io. Object Input Stream; import java. io. ObjectOutputStream; import java. io. Serializable; / ** * This is a simple Java Bean. * @ Author Debadatta Mishra (PIKU) * * / public class Emp implements Serializable ( private static final long serialVersionUID =-164971138528601769L; private String name = null; private int age = 0; private String EmpID = null; private transient Project proj = null; Public (EMP) ( super (); proj = new Project (); ) public String getName () ( Return name; ) public void setName (String name) ( this. name = name; ) public int getAge () ( Return age; ) public void Set (int age) ( this. age = age; ) public String getEmpId () ( EmpID return; ) public void setEmpId (String EmpID) ( this. EmpID = EmpID; ) public project getProj () ( Return proj; ) public void setProj (Project proj) ( this. proj = proj; ) / ** You are proividing a standard callback method * For manual seriallization process. * @ Param os the type (@ link) ObjectOutputStream * @ Throws Exception of type (@ link) Exception * / private void writeObject (ObjectOutputStream os) throws Exception ( try ( os. default Write Object (); os. writeInt (Proj. getProjectId ()); os. writeObject (Proj. getPojectName ()); ) catch (Exception e) ( e. printStackTrace (); ) ) / ** You offer standard with desrialization * Some manual TWIK. * @ Param oin of type (@ link) Object Input Stream * @ Throws Exception of type (@ link) Exception * / private void readObject (Object Input Stream OIN) throws Exception ( try ( . Oin defaultReadObject (); proj = new Project (); proj. setProjectId (oin. readInt ()); proj. setPojectName ((String oin). readObject ()); ) catch (Exception e) ( e. printStackTrace (); ) ) ) The following class name is Project. Java com package. Core. ; Are / ** * @ Author Debadatta Mishra (PIKU) * * / public class Project ( private int projectID = 0; private String pojectName = null; public Project () ( super (); ) public String getPojectName () ( PojectName return; ) public void setPojectName (String pojectName) ( this. pojectName = pojectName; ) public int getProjectId () ( ProjectID return; ) public void setProjectId (int projectID) ( this. projectID = projectID; ) ) The following class name is TestSerialization. Java test environment, the class is. com package. Core. ; Are import java. io. FileInputStream; import java. io. FileOutputStream; import java. io. Object Input Stream; import java. io. ObjectOutputStream; / ** * @ Author Debadatta Mishra (PIKU) * * / public class TestSerialization ( public static void main (String [] args) ( EMP Emp = new EMP (); EMP. SetAge (23); EMP. setEmpId (“A1″); EMP. setName (“John”); Project proj = new Project (); proj. setProjectId (5555); proj. setPojectName (“XYZ”); EMP. setProj (proj); try ( ObjectOutputStream OU = new ObjectOutputStream ( new FileOutputStream (“D: / test. SER”)); Organizational units. writeObject (EMP); Organizational units. close (); Object Input Stream oin = new Object Input Stream (new FileInputStream ( ”D: / test. SER”)); Emp EMP1 = (oin EMP). readObject (); System. out. Println (“EMP age —-” + EMP1. GetAge ()); Project Proj1 = EMP1. getProj (); System. out. Println (“Id Proj —–” + Proj1. GetProjectId ()); System. out. Println (“Proj Name —-” + Proj1. GetPojectName ()); ) catch (Exception e) ( e. printStackTrace (); ) ) ) Please note the two methods writeObject () and readObject () within the class Emp. These two methods are going in the sense of importance that you, in order to achieve serialization with your default object serialization, and the manual with the not serializable object with some data. If you call the methods writeObject () and readObject () are called automatically for a particular object, and these methods will be some default data. In these particular methods you are persistent data manually and by which the whole object serializable. Completion I hope you enjoy my article. If you have any problems or mistakes, please send me an e-mail address in the debadattamishra @ AOL. com. This item is only for those who are new in Java Development meant. This article makes no commercial significance. Please give me feedback on this product.
Apr 102010
