0

i write ArrayLists into a file. I read it with FileInputStream. But always just the "first" ArrayList is appears by reading. I tried it with readInt() / wirteInt(), and loops, but there were always thrown exceptions, by calling readInt() --> EOF I want to read all ArrayList from this File into an ArrayList. My application needs to be persistent, so i serialized the ArrayLists.

write into file:

        try {         
        FileOutputStream fos = new FileOutputStream(_cache, true);
        ObjectOutputStream os = new ObjectOutputStream(fos);
        // os.writeInt(newValueList.size()); // Save size first
        os.writeObject(newValueList); 

        os.flush();
        os.close();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

read from file:

 List cachedValueList = new ArrayList<String>();
    ObjectInputStream o = new ObjectInputStream(new FileInputStream("caching.io"));
  //  int count = o.readInt(); // Get the number of regions

    try {

            cachedValueList.add(o.readObject());

    } catch (EOFException e) {
        o.close();
        e.printStackTrace();
    } 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();
    }
6
  • 1
    Where are your loops? Why don't you simply write a List<ArrayList> instead of looping and writing all the lists one by one? Commented Aug 2, 2012 at 14:40
  • os.writeObject(newValueList); - what is type of newValueList? cachedValueList.add(o.readObject()); - what do you intend to read? It seems that you intend to read a String. I think that you try to write some kind of list and then read a list object and add it to another list (with implicit convertion to String) Commented Aug 2, 2012 at 14:42
  • @JBNizet I suspect the problem is that he is using the same list repeatedly. Commented Aug 2, 2012 at 14:42
  • while ((obj = o.readObject()) != null) { if (obj instanceof ArrayList) { cachedValueList = (List) obj; _historyValueList.add(cachedValueList); } } Commented Aug 2, 2012 at 14:48
  • 1
    @MaxL Note that that loop is incorrect. readObject() doesn't return null at end of stream: it throws EOFException. 'Invalid type code: AC' means you used more than one ObjectOutputStream on the same socket: don't do that. Commented Aug 4, 2012 at 6:37

1 Answer 1

1

If you are writing the same list many times, it will only write the list once. After that it will use the same reference but the contents will be the same (this is true of any object)

If you call reset() between writeObject() it will send a fresh copy of the list each time.

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.