0

I need help with reading an array list from a Serialized text file, I do not believe I am having a problem with it however I cannot see if it is actually storing all of the information because I cant print it out to test it, here are the code snipets: and when I do read from the file I get the ArrayList cannot be cast to StoreAddress

   FileInputStream ios = new FileInputStream("C:\\Users\\Mr Cata\\Desktop\\Testingoutput.txt");
   ObjectInputStream ois = new ObjectInputStream(ios);
   StoreAddress SAR = (StoreAddress)ois.readObject();
   ArrayList<StoreAddress> ALStore = (ArrayList)ois.readObject();

   for(int i = 0; i < ALStore.size(); i++){
        String list = ALStore.get(i).toString();            
   }

   ios.close();

next

for(int i = 0; i < ALStore.size(); i++){
   ALStore.get(i);                  
}

...

for(int i = 0; i < ALStore.size(); i++){
   if (i >= ALStore.size())
   {
        ALStore.add(SA);}
   }

   FileOutputStream fos = new FileOutputStream("C:\\Users\\Mr Cata\\Desktop\\Testingoutput.txt");
   ObjectOutputStream oos = new ObjectOutputStream(fos);

   oos.writeObject(ALStore);
   fos.close();
}

3 Answers 3

0

When you are writing into the file , you write like

for(int i = 0; i < ALStore.size(); i++){
  if (i >= ALStore.size()) {
         ALStore.add(SA);
        }
    }
FileOutputStream fos = new FileOutputStream("C:\\Users\\MrCata\\Desktop\\Testingoutput.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(ALStore);
fos.close();

Here you are writing the

ALStore

which is the array list you are mentioning. But when you are reading, your snippet looks like

FileInputStream ios = new FileInputStream("C:\\Users\\MrCata\\Desktop\\Testingoutput.txt");
 ObjectInputStream ois = new ObjectInputStream(ios);
 StoreAddress SAR = (StoreAddress)ois.readObject();
 ArrayList<StoreAddress> ALStore = (ArrayList)ois.readObject();
 for(int i = 0; i < ALStore.size(); i++){
    String list = ALStore.get(i).toString();

  }

 ios.close();

what you have to notice here is you have have written only array list object, but not StoreAddress. If so, what is the need for the line

 StoreAddress SAR = (StoreAddress)ois.readObject();

in the snippet ? This is the problem here. Remove this and it should work fine.

Sign up to request clarification or add additional context in comments.

Comments

0

1. Create a class with and Instance variable which will an ArrayList<Your_Type>.

2. Create an object of this class, and Serialize the object and store it in "Myfile.ser" .

3. When De-serializing , cast the object to it original type..

eg:

          Cat c = (Cat) inputS.readObject();

4. Now access the ArrayList from this object reference variable.

**eg:**

          c.myArr;

You can print the elements of the arraylist using for-each if you want..like this..

      for (Your_type t : myArr){

             System.out.println(t);

   }

4 Comments

I already have a separate class that serializes, or are you saying make the array list store an array of strings in the serialized class?
First tell me what does your ArrayList holds.... Second dont Serialize the ArrayList, but Serialize the class that holds this ArrayList as an instance variable
The serialized class holds a bunch of strings that are received from a GUI through user input.
Ok..so your are storing a single serialized object with multiple strings, or Mulitple serialized objects with mulitiple strings....?? Whatsoever, store them in an ArrayList and serialize it.. then during deserialization cast it into the Serialized class object type.
0

In the last code snippet you do a single write of an ArrayList<ALStore> however in your first code snippet you try to do two reads, the first one being a StoreAddress instance.

Hence the cast exception.

btw, try to adhere to Java convention of writing variables with camel case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.