This discussion is archived
9 Replies Latest reply: Jun 11, 2010 5:34 AM by jtahlborn RSS

Problem with the externalizable interface

843790 Newbie
Currently Being Moderated
hii i trying a simple example withe externalizable interface but getting EOFException while reading it
can any one tell me where???

     @Override
     public void readExternal(ObjectInput in) throws IOException,
               ClassNotFoundException {
          // TODO Auto-generated method stub
          *this.id=in.readInt();*          this.name=(String)in.readObject();
          //this.setSalary(2000);
     }
i am getting error in the bold(while reading int)
public static void main(String[] args)
     {
     
          
          externaliz s1=new externaliz(7344, "Gourav",2000);
          try
          {
               ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(new File("external.txt")));
               oos.writeObject(s1);
          
               ObjectInputStream ois=new ObjectInputStream(new FileInputStream(new File("external.txt")));
               externaliz s2=(externaliz)ois.readObject();
               System.out.println(s2);
          }
          catch (FileNotFoundException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
          catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          } 
          catch (ClassNotFoundException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
}
  • 1. Re: Problem with the externalizable interface
    EJP Guru
    Currently Being Moderated
    What does your writeExternal() method look like?
  • 2. Re: Problem with the externalizable interface
    843790 Newbie
    Currently Being Moderated
         @Override
         public void writeExternal(ObjectOutput out) throws IOException {
              // TODO Auto-generated method stub
              out.writeObject(this);
         }
  • 3. Re: Problem with the externalizable interface
    843790 Newbie
    Currently Being Moderated
    package test;
    
    import java.io.Externalizable;
    import java.io.IOException;
    import java.io.ObjectInput;
    import java.io.ObjectOutput;
    
    public class externaliz implements Externalizable
    {
         int id;
         String name;
         transient int salary;
         
         public externaliz(int id, String name ,int salary)
         {
              super();
              this.id = id;
              this.name = name;
              this.salary=salary;
         }
    
         public externaliz() {
              // TODO Auto-generated constructor stub
         }
    
         public int getSalary() {
              return salary;
         }
    
         public void setSalary(int salary) {
              this.salary = salary;
         }
         
         public int getId() {
              return id;
         }
         public void setId(int id) {
              this.id = id;
         }
         public String getName() {
              return name;
         }
         public void setName(String name) {
              this.name = name;
         }
         
         public String toString()
         {
              return id+"   "+name+"  "+salary;
         }
         
         
         @Override
         public void readExternal(ObjectInput in) throws IOException,
                   ClassNotFoundException {
              // TODO Auto-generated method stub
              this.id=in.readInt();
              this.name=(String)in.readObject();
              //this.setSalary(2000);
         }
    
    
    
         @Override
         public void writeExternal(ObjectOutput out) throws IOException {
              // TODO Auto-generated method stub
              out.writeObject(this);
         }
    }
    complete code
  • 4. Re: Problem with the externalizable interface
    843790 Newbie
    Currently Being Moderated
    @Override
         public void readExternal(ObjectInput in) throws IOException,
                   ClassNotFoundException {
              // TODO Auto-generated method stub
              //this.id=System.out.println(in.readInt());
              //this.name=(String)in.readObject();
              //this.setSalary(2000);
              externaliz e1=(externaliz)in.readObject();
              System.out.println(e1);
         }
    this give me output 0 null 0
    why it gives me defult value for the object.
    any idea any hint!!!! :(
  • 5. Re: Problem with the externalizable interface
    843790 Newbie
    Currently Being Moderated
    You need to write and read data using specified methods (readInt(), writeInt() for int type) and do the reading/writing the object in the same order:
            @Override
         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
              this.name = (String) in.readObject();
              this.id = in.readInt();
         }
    
         @Override
         public void writeExternal(ObjectOutput out) throws IOException {
              out.writeObject(name);
              out.writeInt(id);
         }
    This should work. Also remember that the name of the class should start with a capital letter.
  • 6. Re: Problem with the externalizable interface
    EJP Guru
    Currently Being Moderated
         @Override
              public void readExternal(ObjectInput in) throws IOException,
                        ClassNotFoundException {
                   // TODO Auto-generated method stub
                   this.id=in.readInt();
                   this.name=(String)in.readObject();
                   //this.setSalary(2000);
         @Override
         public void writeExternal(ObjectOutput out) throws IOException {
              // TODO Auto-generated method stub
              out.writeObject(this);
         }
    So why are you expecting to be able to read something you didn't write?

    If you want to have readInt() and readObject() in your readExternal() method, you have to do the corresponding writeInt() and writeObject() in your writeExternal() method. Surely that's obvious?

    Why exactly are you implementing Externalizable at all?
  • 7. Re: Problem with the externalizable interface
    843790 Newbie
    Currently Being Moderated
    thanks ejp and elOpalo
    now its work

    and i read that using externalizable interface you can add extra info
    i just want to try that
    but before that i stuck into that
  • 8. Re: Problem with the externalizable interface
    EJP Guru
    Currently Being Moderated
    and i read that using externalizable interface you can add extra info
    Sure you can, and you are also completely responsible for serializing and deserializing every field of the class and all its serializable superclasses, as far as the eye can reach. Why would you bother?

    Best not to use these things until you know you need them.
  • 9. Re: Problem with the externalizable interface
    jtahlborn Expert
    Currently Being Moderated
    GouravBansal wrote:
    thanks ejp and elOpalo
    now its work

    and i read that using externalizable interface you can add extra info
    i just want to try that
    but before that i stuck into that
    you can add extra information using plain old Serializable as well, you just need custom readObject and writeObject methods, and as a bonus, you don't need to write the logic for handling the basic stuff. i would suggest sticking with Serializable unless you need absolute control over the serialization process, at which point you would use Externalizable.