Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Read more than one object from a file!

Reply
Thread Tools

Read more than one object from a file!

 
 
tranky
Guest
Posts: n/a
 
      01-11-2006
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!


 
Reply With Quote
 
 
 
 
Thomas Hawtin
Guest
Posts: n/a
 
      01-11-2006
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/
 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      01-12-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can one declare more than one signal on one line? Merciadri Luca VHDL 4 11-01-2010 02:00 PM
Like all great travelers, I have seen more than I remember andremember more than I have seen. shenrilaa@gmail.com Java 0 03-06-2008 08:11 AM
Like all great travelers, I have seen more than I remember andremember more than I have seen. shenrilaa@gmail.com C++ 0 03-05-2008 08:41 AM
Like all great travelers, I have seen more than I remember andremember more than I have seen. shenrilaa@gmail.com C Programming 0 03-05-2008 03:26 AM
how to check the scanf function if it will read more than one number moosdau C Programming 51 01-04-2006 12:45 AM



Advertisments