Binary files with array objects help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • milk242
    New Member
    • Feb 2009
    • 25

    #1

    Binary files with array objects help

    I know I'm making a mistake, but I'm wondering if someone could tell me the type of mistake I'm making. I know I can write the whole array to a file, but I want to create separate binary files for each Customer. Then I want to readout the binary file... either one or all of them.

    Could someone help me? Thanks.

    Code:
    Customer[] oneArray = new Customer[2];
    		oneArray[0] = new Customer("John", "email@gmail.com", "123-345-1234", "1, 2");
    		oneArray[1] = new Customer("Smith", "email2@gmail.com", "234-456-1234", "1, 2, 3");
    		
    		String fileName1 = "Data/Customers/John.dat";
    		String fileName2 = "Data/Customers/Smith.dat";
    		
    		try
    		{
    			ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(fileName1));
    			outputStream.writeObject(oneArray[0]);
    			outputStream.close();
    			ObjectOutputStream outputStream2 = new ObjectOutputStream(new FileOutputStream(fileName2));
    			outputStream2.writeObject(oneArray[1]);
    			outputStream2.close();
    		}
    		catch(IOException e)
    		{
    			System.out.println("Error writing to file.");
    			System.exit(0);
    		}
    		
    		Customer[] anotherArray = null;
    		try
    		{
    			ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(fileName1));
    			anotherArray[0] = (Customer)inputStream.readObject();
    			inputStream.close();
    			ObjectInputStream inputStream2 = new ObjectInputStream(new FileInputStream(fileName2));
    			anotherArray[1] = (Customer)inputStream2.readObject();
    			inputStream2.close();
    		}
    		catch(Exception e)
    		{
    			System.out.println("Error reading file.");
    			System.exit(0);
    		}
    		
    		System.out.println(anotherArray[0]);
    		System.out.println(anotherArray[1]);
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You array 'anotherArray' is null; allocate the array befire you dereference it.

    kind regards,

    Jos

    Comment

    • milk242
      New Member
      • Feb 2009
      • 25

      #3
      So make it Customer[] anotherArray = new Customer[2]; ?

      Also I tried to rework the code... I thought this would work, but it doesn't... any idea?

      Code:
      for (int i = 0; i < 3; i++);
      		{
      			String cusName = null, cusEmail = null, cusPhone = null, cusServ = null;
      			System.out.println("Customer information:");
      			System.out.print("Name: ");
      			cusName = keyboard.nextLine();
      			System.out.print("Email: ");
      			cusEmail = keyboard.nextLine();
      			System.out.print("Number: ");
      			cusPhone = keyboard.nextLine();
      			System.out.print("Services: ");
      			cusServ = keyboard.nextLine();
      			
      			Customer myCustomer[i] = new Customer("cusName", "cusEmail", "cusPhone", "cusServ");
      
      			String fileName1 = myCustomer[i]+".dat";
      
      			try
      			{
      				ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(fileName1));
      				outputStream.writeObject(myCustomer[i]);
      				outputStream.close();
      			}
      			catch(IOException e)
      			{
      				System.out.println("Error writing to file " + fileName1);
      				System.exit(0);
      			}
      		}
      		
      		
      		for (int i = 0; i < 3; i++);
      		{
      			Customer array[i] = new Customer();
      			try
      			{
      				fileName1 = myCustomer[i]+".dat";
      				ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(fileName1));
      				array[i] = (Customer)inputStream.readObject();
      				inputStream.close();
      				System.out.println(array[i]);
      			}
      			catch(Exception e)
      			{
      				System.out.println("Error reading file.");
      				System.exit(0);
      			}
      		}

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        If an Exception was thrown add a statement e.printStackTra ce() to your catch clause and you'll know exactly what went wrong.

        kind regards,

        Jos

        ps. your code doesn't compile; read the compiler diagnostic messages.

        Comment

        • milk242
          New Member
          • Feb 2009
          • 25

          #5
          Do you have any idea what this means?

          Error reading file Data/Customers/Will.dat.
          java.io.Invalid ClassException: Customer; local class incompatible: stream classdesc serialVersionUI D = -585219468501467 1947, local class serialVersionUI D = -492827965364602 4047
          at java.io.ObjectS treamClass.init NonProxy(Object StreamClass.jav a:546)
          at java.io.ObjectI nputStream.read NonProxyDesc(Ob jectInputStream .java:1552)
          at java.io.ObjectI nputStream.read ClassDesc(Objec tInputStream.ja va:1466)
          at java.io.ObjectI nputStream.read OrdinaryObject( ObjectInputStre am.java:1699)
          at java.io.ObjectI nputStream.read Object0(ObjectI nputStream.java :1305)
          at java.io.ObjectI nputStream.read Object(ObjectIn putStream.java: 348)
          at Customer.listCu stomers(Custome r.java:183)
          at Project.main(Pr oject.java:9)

          Here is the code that it's running.

          Code:
          	public void listCustomers()
          	{
          		Customer anotherArray = null;
          		String[] customersArray = null;
          		int numOfEntries = 0;
          		String fileName = null;
          		
          		try
          		{
          			ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("Data/customers.dat"));
          			customersArray = (String[])inputStream.readObject();
          			inputStream.close();
          		}
          		catch(Exception e)
          		{
          			System.out.println("Error reading file customers.dat.");
          			System.exit(0);
          		}
          		
          		try
          		{	
          			ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("Data/entries.dat"));
          			numOfEntries = inputStream.readInt();
          			inputStream.close();
          		}
          		catch(EOFException e)
          		{}
          		catch(IOException e)
          		{
          			System.out.println("Problem opening the file entries.dat");
          			System.exit(0);
          		}
          
          		try
          		{
          			ObjectInputStream inputStream = null;
          			for (int i = 1; i <= numOfEntries; i++)
          			{
          				fileName = "Data/Customers/" + customersArray[i] + ".dat";
          				inputStream = new ObjectInputStream(new FileInputStream(fileName));
          				anotherArray = (Customer)inputStream.readObject();
          				System.out.println(anotherArray);
          			}
          			inputStream.close();
          		}
          		catch(Exception e)
          		{
          			System.out.println("Error reading file " + fileName + ".");
          			e.printStackTrace();
          			System.exit(0);
          		}
          	}
          I'm not sure if I'm making an error in creating the binary files or if I'm making an error reading the binary files. And it's strange because it'll be able to read the binary file for a while and then suddenly stop and throw the error.

          Here is the code to create the files:

          Code:
          public void addCustomer()
          	{
          		Scanner keyboard = new Scanner(System.in);
          		boolean moreEntries = true;
          		
          		while (moreEntries)
          		{
          			listServices();
          			System.out.println("Customer information:");
          			System.out.print("Name: ");
          			name = keyboard.nextLine();
          			System.out.print("Email: ");
          			email = keyboard.nextLine();
          			System.out.print("Number: ");
          			number = keyboard.nextLine();
          			System.out.print("Services: ");
          			service = keyboard.nextLine();
          			
          			cusName[entries] = name;
          			myCustomer[entries] = new Customer(name, email, number, service);
          			String fileName = "Data/Customers/"+getName() + ".dat";
          			
          			try
          			{
          				ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(fileName));
          				outputStream.writeObject(myCustomer[entries]);
          				outputStream.close();
          			}
          			catch(IOException e)
          			{
          				System.out.println("Error writing to file " + fileName + ".");
          				System.exit(0);
          			}
          			
          			try
          			{
          				ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("Data/customers.dat"));
          				outputStream.writeObject(cusName);
          				outputStream.close();
          			}
          			catch(IOException e)
          			{
          				System.out.println("Error writing to file customers.dat.");
          				System.exit(0);
          			}
          			
          			try
          			{
          				ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("Data/entries.dat"));
          				outputStream.writeInt(entries);
          				outputStream.close();
          			}
          			catch(IOException e)
          			{
          				System.out.println("Error writing to file entries.dat.");
          				System.exit(0);
          			}
          			
          			System.out.println();
          			System.out.println("More customers to add?");
          			String ans = keyboard.nextLine();
          			if (ans.trim().equalsIgnoreCase("no"))
          				moreEntries = false;
          			else
          				entries++;
          		}
          	}
          Thanks for the help so far

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            The first parts of your stack trace says it all. You have class version issues because your classes have different serialVersionUI D values.

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              Originally posted by milk242

              Code:
              for (int i = 0; i < 3; i++);
              		{
              			Customer myCustomer[i] = new Customer("cusName", "cusEmail", "cusPhone", "cusServ");
              
              		}
              		
              		
              		for (int i = 0; i < 3; i++);
              		{
              			Customer array[i] = new Customer();
              		}

              Do these two line compile?
              You can write this,
              Code:
              Customer[] myCustomer = new Customer[n];
              for(int i=0;i<n;i++)
              {
               myCustomer[i] = new Customer(....);
              }

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                Originally posted by r035198x
                The first parts of your stack trace says it all. You have class version issues because your classes have different serialVersionUI D values.
                What does it mean (by serialVersionUI D)?

                Well i went through this site,
                Code:
                class MySerializableClass implements Serializable{
                private static final long serialVersionUID = 7526472295622776147L
                ....
                ....
                }
                How can i make sure that this number can't collide with other Serializable classes.
                If i don't provide this field then does Compiler automatically insert the Serial Number.
                And if i don't change the Serial Number across every class modifications then InvalidClassExc eption will not be thrown. No matter the Class gets changed or not but the Serial Number must be same as per modification.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by dmjpro
                  What does it mean (by serialVersionUI D)?
                  Read all about it in the API documentation for the 'Serializable' interface.

                  kind regards,

                  Jos

                  ps. that link is completely broken; proofread your replies before you post.

                  Comment

                  • dmjpro
                    Top Contributor
                    • Jan 2007
                    • 2476

                    #10
                    Actually from which version of Java serialVersionUI D introduced? I read the Java 1.3, i couldn't see the term serialVersionUI D in documentation.

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by dmjpro
                      Actually from which version of Java serialVersionUI D introduced? I read the Java 1.3, i couldn't see the term serialVersionUI D in documentation.
                      Who cares? My documentation says that the Serializable interface was introduced in JDK version 1.1 and that's what we have to live with. The OP didn't put that version ID in his classes (which he should have).

                      The cure for the OP's problems is to throw away the old data file and create a new one because (most likely) the class was changed structurally.

                      kind regards,

                      Jos

                      Comment

                      • milk242
                        New Member
                        • Feb 2009
                        • 25

                        #12
                        Thanks for all your help, I added a serializedid to my class to fix the problem

                        Comment

                        • dmjpro
                          Top Contributor
                          • Jan 2007
                          • 2476

                          #13
                          Originally posted by JosAH
                          Who cares? My documentation says that the Serializable interface was introduced in JDK version 1.1 and that's what we have to live with. The OP didn't put that version ID in his classes (which he should have).

                          The cure for the OP's problems is to throw away the old data file and create a new one because (most likely) the class was changed structurally.

                          kind regards,

                          Jos
                          I am not talking about Serializable interface but about the serialID.
                          Have a look at this API Doc and this API Doc.
                          I could not find out the serialVersionUI D in the Java 1.4 API Doc ;)

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by dmjpro
                            I am not talking about Serializable interface but about the serialID.
                            Have a look at this API Doc and this API Doc.
                            I could not find out the serialVersionUI D in the Java 1.4 API Doc ;)
                            See my reply #9; I didn't write that to be ignored. Java 1.4 has gone through its end of life cycle and it is long dead; there is no need to discuss it.

                            kind regards,

                            Jos

                            Comment

                            Working...