Results 1 to 4 of 4
  1. #1
    Rio123 is offline Member
    Join Date
    Aug 2014
    Posts
    3
    Rep Power
    0

    Thumbs down Deserializing two files.

    Java Code:
    import java.io.EOFException;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.sql.*;
    
    
    public class TestSerializer
    {
         
       public static void main(String [] args)
       {
    
         
         Customer c = null;
         Dvd m = null;
           try
           {
              FileInputStream fileIn  = new FileInputStream("src/Client.ser");
              ObjectInputStream customerIn = new ObjectInputStream(fileIn);
              FileInputStream fileIn1  = new FileInputStream("src/Movies.ser");
              ObjectInputStream movieIn= new ObjectInputStream(fileIn);
        while(true)
        {
           c = (Customer) customerIn.readObject();
           
           System.out.println("Deserialized Customer...");
           System.out.println("Name: " + c.getName());
           System.out.println("Surname: " + c.getSurname());
           System.out.println("Phone number: " + c.getPhoneNum());
           System.out.println("Credit: " + c.getCredit());
             
          
            m = (Dvd) movieIn.readObject();
           
           System.out.println("Deserialized Movie...");
           System.out.println("Name: " + m.getTitle());
           //System.out.println("Catergory: " + m.getCategory());
          
            
        
           customerIn.close();
           fileIn.close();
           movieIn.close();
           fileIn1.close();
        }
        
           }
           catch(Exception ex)
           {
               System.out.println(ex.getMessage());
           }
       }
    }
    Last edited by Rio123; 10-16-2014 at 03:16 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Deserializing two files.

    First, place your code between code tags to provide formatting [code][/code]. Then focus on reading only one object. If that doesn't
    work, then try the next object. One, the other, or both could be corrupted. And show what line the error was from.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Rio123 is offline Member
    Join Date
    Aug 2014
    Posts
    3
    Rep Power
    0

    Default Re: Deserializing two files.

    Okay thanks. Just did that and i will see if the serialized files works if i do it individually

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Deserializing two files.

    Just to expand on jim's post, what the above is doing is:
    Java Code:
    while(true) {
        read an object from client file
    
        read an object from movie file
    }
    Either of those reads can throw the exception that occurs when there are no more objects in the file so, unless you have exactly the same number of clients and movies, you will only read one set completely and the other will be short.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Deserializing dynamically-loaded classes
    By kjkrum in forum Advanced Java
    Replies: 0
    Last Post: 10-08-2012, 04:32 AM
  2. Replies: 7
    Last Post: 01-12-2012, 07:34 PM
  3. CannotResolveClassException while deserializing
    By harry1985 in forum New To Java
    Replies: 6
    Last Post: 07-27-2011, 12:45 PM
  4. Replies: 2
    Last Post: 06-11-2011, 01:54 PM
  5. Replies: 0
    Last Post: 12-22-2008, 06:16 PM

Posting Permissions

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