简体   繁体   中英

readObject method is not being called

I am trying to test the custom serialization but readObject() method is not being called.Although I am able to call the writeObject method. Can we overload the readObject method().

    public class Test {

        String pwd1 ;

        public static void main(String arg[]) throws IOException, ClassNotFoundException{

            FileInputStream fis = new FileInputStream("//vhub.lmera.ericsson.se//home//xsinkus//WDP//Desktop2k8R2//COMP_DOCS//Test//kusha1.ser");
            @SuppressWarnings("resource")
            ObjectInputStream ois = new ObjectInputStream(fis);
            System.out.println("I am being called ");
            SerilizationTest si = (SerilizationTest)ois.readObject();

            System.out.println("After called ");
            System.out.println(si.name);
            System.out.println(si.pwd);
            //si.readObject();
        }

          private void readObject(ObjectInputStream ois)
                    throws IOException, ClassNotFoundException {
              ois.defaultReadObject();
                     pwd1  = (String) ois.readObject();
                     System.out.println("I am in ");
                  }
    }

Your readObject method needs to reside inside the class that implements Serializable . This way, only that class reads objects in this user defined manner.

Basically, it's not calling your method because it has no idea it exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM