Serialization of a single Case object with Java native interface serializable

Source: Internet
Author: User
Tags parent directory serialization static class stub
a single instance class, which is also the class that will be serialized
Package test;
Import java.io.Serializable;

Import Java.util.Date; /** * Defines a serializable single instance class, which is implemented by a static inner class implementation to deserialize the acquired object or the single instance pattern after serialization.
 With the Readresolve method * For this is not very understanding. * * * @author Cindy * */public class MyObject implements Serializable {/** * sequence code * * private static

    Final long serialversionuid = 8881L;
    Private String name = "";
    Private Date birthday = null;

    Private String address = "";
    Public String GetName () {return name;
    public void SetName (String name) {this.name = name;
    Public Date Getbirthday () {return birthday;
    The public void Setbirthday (Date birthday) {this.birthday = birthday;
    Public String getaddress () {return address;
    public void setaddress (String address) {this.address = address;
    public static long Getserialversionuid () {return serialversionuid; Private MyObject () {//TODO auto-generated constructor Stub}//internal class to implement a single example mode private static class Myobjecthandler {private static final MyObject MyObject =
    New MyObject ();

    public static MyObject getinstance () {return myobjecthandler.myobject;
        } protected Object Readresolve () {System.out.println ("Readresolve method invoked");
    return myobjecthandler.myobject;
 }

}
File Service class

Serialization is the persistence of data in a file

Package file.service;
Import Java.io.File;

Import java.io.IOException;

Import test.run.SerialAndUnserial; public class Fileservice {public Fileservice () {//TODO auto-generated constructor stub} public s 

        Tatic File createandopenfile (String filepath) {if (filepath = = NULL | | filepath = "") {return null;
        String BasePath = SerialAndUnserial.class.getResource ("/"). GetFile ();
        Here you can see where the classpath address is System.out.println ("classpath path" + basepath);

        File file = null; /** * has the getcanonicalfile effect is to include the jump to the upper directory. Remove form a table of contents that can be parsed to form/home/cindy/eclipseworkspace/singleton_serial/txt/object.
         TXT * This is the case before the Getcanonicalfile () method is called. */home/cindy/eclipseworkspace/singleton_serial/. /txt/object.txt * You can get rid of that method and try it.
         Seemingly removed can also be performed normally.
        */try {file = new file (BasePath, filepath). Getcanonicalfile ();
     catch (IOException E2) {       TODO auto-generated Catch block E2.printstacktrace ();

        } System.out.println ("Parent directory of the path" + file.getparent ());
        Determines whether the path represented by this abstract path name exists System.out.println (file.exists ());
            if (!file.exists ()) {//All missing parent directories are created File.getparentfile (). Mkdirs ();
                try {//Last Create file File.createnewfile ();
            return file;
            catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
        } System.out.println (File.exists ());
    return file;
 }

}
Test Cases
Package test.run;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutputStream;

Import Java.util.Date;
Import File.service.FileService; Import Test.

MyObject;

    public class Serialandunserial {public serialandunserial () {//TODO auto-generated constructor stub} @SuppressWarnings ("deprecation") public static void main (string[] args) {MyObject MyObject = Myobject.get

        Instance ();
        Myobject.setaddress ("Guangzhou Fanyu District Guangdong University of Technology");
        Myobject.setbirthday (New Date (2012, 0, 21));
        Myobject.setname ("Vincent"); String RelativePath = "..
        /txt/object.txt ";
        System.out.println (SerialAndUnserial.class.getResource ("/"). GetFile ());
        System.out.println (SerialAndUnserial.class.getClassLoader (). GetResource ("/"). GetFile ());
        File File = Fileservice.createandopenfile (RelativePath);
        System.out.println (File.getpath ()); Try {FileOutputStream fos = new FileOutputStream (file);
            ObjectOutputStream oos = new ObjectOutputStream (FOS);
            Oos.writeobject (MyObject);
            Oos.close ();
            Fos.close ();
            SYSTEM.OUT.PRINTLN ("hash value before MyObject instance serialization" + Myobject.hashcode ());

        System.out.println (myobject.getaddress () + "T" + myobject.getname () + "T" + myobject.getbirthday ());
        catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
            Finally {} try {FileInputStream fis = new FileInputStream (file);
            ObjectInputStream ObjectInputStream = new ObjectInputStream (FIS);
            MyObject Mobject = (MyObject) objectinputstream.readobject ();
            Objectinputstream.close ();
            Fis.close ();
            SYSTEM.OUT.PRINTLN ("hash value after MyObject instance serialization" + Mobject.hashcode ()); System.out.println (mobject.getaddress () + "T" + mobject.getname () + "T" + mobject.getbirthday ());
        catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
 }

    }
}

The last rendered result

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.