简体   繁体   English

未调用readObject方法

[英]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. 我正在尝试测试自定义序列化,但未调用readObject()方法。尽管我能够调用writeObject方法。 Can we overload the readObject method(). 我们可以重载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 . 您的readObject方法需要驻留在实现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. 基本上,它不调用您的方法,因为它不知道它的存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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