Results 1 to 4 of 4

Thread: Deserialization

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Resolved Deserialization

    Any ideas on how i could go about deserializing SerObj?
    Code:
      import java.io.*; 
     
      class SerObj implements Serializable{
        transient private int temp; 
        private boolean indicator; 
         
        public SerObj(int temp, boolean indicator){
         this.temp = temp; 
         this.indicator = indicator;
        }
        public int getTemp(){
            return temp; 
        } 
        public boolean getIndicator(){
          return indicator; 
        }
      }
    
     public class X{
      public static void main(String[] args){
      ObjectOutputStream oup = null;   
      try{
        oup = new ObjectOutputStream(new FileOutputStream("C:" + File.separator + "ObjSerFile")); 
         oup.writeObject(new SerObj(68,true));   
       }catch(IOException io){
           System.err.println(io); 
       }finally{
        try{
         oup.close(); 
         }catch(IOException io){
           System.err.println(io);     
        }
       }
      }
     }

  2. #2

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I have this but a java.io.EOFException is thrown.
    Code:
     import java.io.*; 
     
     public class Y{
      public static void main(String[] args){
      ObjectInputStream oip = null;     
      try{
        oip = new ObjectInputStream(new FileInputStream("C:" + File.separator + "ObjSerFile")); 
        int temp = oip.readInt(); 
        boolean indicator = oip.readBoolean();  
        SerObj serobj = new SerObj(temp, indicator); 
        System.out.println(serobj.getTemp());  
        System.out.println(serobj.getIndicator()); 
       }catch(IOException io){
           System.err.println(io); 
       }finally{
        try{
         oip.close(); 
        }catch(IOException io){
          System.err.println(io);     
        } 
       }     
      }

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    SerObj so = (SerObj)oip.readObject();

    There are two problems with your approach. First, classes are specially encoded, so you can't just read the members back. Second, temp is marked as transient and thus doesn't get saved, so even if you could read the members back, there wouldn't be anything to read.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Ok works fine. Thanks.
    Code:
    import java.io.*; 
     
     public class Y{
       public static void main(String[] args){
       ObjectInputStream oip = null;     
       try{
         oip = new ObjectInputStream(new FileInputStream("C:" + File.separator + "ObjSerFile")); 
         SerObj serobj = (SerObj)oip.readObject(); 
         System.out.println(serobj.getTemp());  
         System.out.println(serobj.getIndicator()); 
        }catch(Exception io){
           System.err.println(io);
        }finally{
       try{
        oip.close(); 
       }catch(IOException io){
         System.err.println(io);     
       } 
      }     
     }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width