Read more than one object from a file!

Discussion in 'Java' started by tranky, Jan 11, 2006.

  1. tranky

    tranky Guest

    I've serializated more than one object inside a file with writeObject. It's
    possible, now, to read that objects with the method readObject?!
    I'm able to read the first object, not the others!

    Can you help me?!?

    thank u!
     
    tranky, Jan 11, 2006
    #1
    1. Advertising

  2. tranky wrote:
    > I've serializated more than one object inside a file with writeObject. It's
    > possible, now, to read that objects with the method readObject?!
    > I'm able to read the first object, not the others!
    >
    > Can you help me?!?


    I think first we'll need to know what is wrong. It should work fine.
    Below is some code I wrote the other day (for production use it'd close
    the streams within a finally, for instance).

    Tom Hawtin

    import java.io.*;
    import java.util.*;

    class Save {
    public static void main(String[] args) throws IOException {
    ObjectOutputStream out = new ObjectOutputStream(
    new FileOutputStream("checked.ser")
    );
    Map<String,Integer> map = new HashMap<String,Integer>();
    map.put("a", 1);
    map.put("b", 2);
    out.writeObject(
    Collections.checkedMap(map, String.class, Integer.class)
    );
    List<Long> list = new ArrayList<Long>();
    list.add(5L);
    list.add(6L);
    out.writeObject(Collections.checkedList(list, Long.class));
    out.writeObject((Short)(short)42);
    out.close();
    }
    }

    import java.io.*;
    import java.util.*;

    class Load {
    public static void main(String[] args) throws Exception {
    ObjectInputStream in = new ObjectInputStream(
    new FileInputStream("checked.ser")
    );
    Map<String,Integer> map = (Map<String,Integer>)in.readObject();

    System.out.println(map.getClass());
    for (Map.Entry<String,Integer> entry : map.entrySet()) {
    System.out.println(entry.getKey()+" - "+entry.getValue());
    }

    List<Long> list = (List<Long>)in.readObject();
    System.out.println(list.getClass());
    for (Long value : list) {
    System.out.println(value);
    }
    System.out.println(in.readObject());
    }
    }
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Jan 11, 2006
    #2
    1. Advertising

  3. On Wed, 11 Jan 2006 22:24:51 +0100, tranky wrote:
    > I've serializated more than one object inside a file with
    > writeObject. It's possible, now, to read that objects with the
    > method readObject?! I'm able to read the first object, not the
    > others!


    Did you write them all using the *same* ObjectOutputStream, and are
    you attempting to read them using the *same* ObjectInputStream?

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Jan 12, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sachin
    Replies:
    1
    Views:
    355
    Tjerk Wolterink
    Dec 1, 2005
  2. =?ISO-8859-15?Q?Manfred_Preu=DFig?=

    Making one HTML page from more than one file

    =?ISO-8859-15?Q?Manfred_Preu=DFig?=, Apr 19, 2007, in forum: HTML
    Replies:
    18
    Views:
    640
    Steven Saunderson
    Apr 24, 2007
  3. Merciadri Luca
    Replies:
    4
    Views:
    821
  4. Steven D'Aprano
    Replies:
    0
    Views:
    99
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    89
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page