HashMap Conversion

Discussion in 'Java' started by andrewzzz, Nov 11, 2006.

  1. andrewzzz

    andrewzzz Guest

    Hi guys,
    how do i convert an hash map into a byte array(being able to restore
    this later , without losing data)?
    thanks a lot!
    bye
     
    andrewzzz, Nov 11, 2006
    #1
    1. Advertising

  2. andrewzzz wrote:
    > how do i convert an hash map into a byte array(being able to restore
    > this later , without losing data)?


    Serialization !

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Nov 11, 2006
    #2
    1. Advertising

  3. andrewzzz

    andrewzzz Guest

    On 11 Nov, 20:39, Arne Vajhøj <> wrote:
    > andrewzzz wrote:
    > > how do i convert an hash map into a byte array(being able to restore
    > > this later , without losing data)?Serialization !

    >
    > Arne


    can you send me link where to find out how?
    thanks arne!
     
    andrewzzz, Nov 11, 2006
    #3
  4. andrewzzz wrote:
    > On 11 Nov, 20:39, Arne Vajhøj <> wrote:
    >> andrewzzz wrote:
    >>> how do i convert an hash map into a byte array(being able to restore
    >>> this later , without losing data)?Serialization !

    >
    > can you send me link where to find out how?


    private static byte[] serialize(Object o) throws IOException {
    ByteArrayOutputStream ba = new ByteArrayOutputStream(1000);
    ObjectOutputStream oba = new ObjectOutputStream(ba);
    oba.writeObject(o);
    return ba.toByteArray();
    }

    private static Object deserialize(byte[] b) throws IOException,
    ClassNotFoundException {
    ByteArrayInputStream ba = new ByteArrayInputStream(b);
    ObjectInputStream oba = new ObjectInputStream(ba);
    return oba.readObject();
    }

    All objects need to be serializable for this to work though !

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Nov 11, 2006
    #4
  5. andrewzzz

    andrewzzz Guest

    On 11 Nov, 20:53, Arne Vajhøj <> wrote:
    > andrewzzz wrote:
    > > On 11 Nov, 20:39, Arne Vajhøj <> wrote:
    > >> andrewzzz wrote:
    > >>> how do i convert an hash map into a byte array(being able to restore
    > >>> this later , without losing data)?Serialization !

    >
    > > can you send me link where to find out how? private static byte[] serialize(Object o) throws IOException {

    > ByteArrayOutputStream ba = new ByteArrayOutputStream(1000);
    > ObjectOutputStream oba = new ObjectOutputStream(ba);
    > oba.writeObject(o);
    > return ba.toByteArray();
    > }
    >
    > private static Object deserialize(byte[] b) throws IOException,
    > ClassNotFoundException {
    > ByteArrayInputStream ba = new ByteArrayInputStream(b);
    > ObjectInputStream oba = new ObjectInputStream(ba);
    > return oba.readObject();
    > }
    >
    > All objects need to be serializable for this to work though !
    >
    > Arne




    thanks so much! I will try now!
     
    andrewzzz, Nov 11, 2006
    #5
    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. Sanjay Kumar

    HashMap

    Sanjay Kumar, Jul 4, 2003, in forum: Java
    Replies:
    2
    Views:
    3,517
    Sanjay Kumar
    Jul 5, 2003
  2. Jon Skeet
    Replies:
    5
    Views:
    2,194
    Dale King
    Jul 8, 2003
  3. Vince Darley
    Replies:
    4
    Views:
    4,563
    emilchacko
    Mar 2, 2010
  4. Rakesh
    Replies:
    10
    Views:
    12,272
    Mike Schilling
    Apr 8, 2008
  5. , India
    Replies:
    2
    Views:
    509
    Fraser Ross
    Sep 15, 2009
Loading...

Share This Page