Results 1 to 6 of 6
Like Tree3Likes
  • 1 Post By Tolls
  • 1 Post By benji2505
  • 1 Post By jim829

Thread: Deserialize an array of bytes back to List<List<String>>

  1. #1
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    89
    Rep Power
    0

    Default Deserialize an array of bytes back to List<List<String>>

    I have a multidimensional List, List<List<String>>. In order to save it using Preferences API, I would like to serialize it to an array of bytes and then deserialize it to load.

    For saving it, I can use something like (https://stackoverflow.com/questions/...erialization):

    Java Code:
    private void saveValues()
    {
        try 
       {
           ByteArrayOutputStream baos = new ByteArrayOutputStream();
           ObjectOutputStream out = new ObjectOutputStream(baos);
           out.writeObject(myArray);
           byte[] bytes = baos.toByteArray();
           prefs.putByteArray("Data", bytes);
       } 
       catch (IOException ex) 
       {
           Logger.getLogger(Model_WindsAloft.class.getName()).log(Level.SEVERE, null, ex);
       }
    }
    Now, how do I deserialize it properly back to List<List<String>>?

    If it is not multidimensional, say List<String>, I could do this:


    Java Code:
    List<String> myArrayCopy = new ArrayList<>();
    try 
    {
        byte[] bytes = prefs.getByteArray("Data", null);
                
        if(bytes != null)
        {
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            DataInputStream in = new DataInputStream(bais);
            while (in.available() > 0) 
            {
                String element = in.readUTF();
                myArrayCopy.add(element);
            }
                    
            for(int i=0; i<myArrayCopy.size();i++)
            {
                 myArray.set(i, myArrayCopy.get(i));
            }
        }
        else
        {
            // Do what is needed.
         }
    } 
    catch (IOException ex) 
    {
         // Do what is needed.
    }
    But what about the multidimensional list?

    Thank you!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    25

    Default Re: Deserialize an array of bytes back to List<List<String>>

    You've saved the List using an ObjectOutputStream, so why are you reading it in using a DataInputStream?
    Why are you not reading it in using an ObjectInputStream?
    benji2505 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    89
    Rep Power
    0

    Default Re: Deserialize an array of bytes back to List<List<String>>

    Quote Originally Posted by Tolls View Post
    You've saved the List using an ObjectOutputStream, so why are you reading it in using a DataInputStream?
    Why are you not reading it in using an ObjectInputStream?
    Hi, Tolls. I am not reading anything yet. I just provided an example of how I read a unidimensional list. I am stuck trying to understand how to read multidimensional list (List<List<String>>) using ObjectInputStream or another class.

    Thank you!

  4. #4
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    390
    Rep Power
    4

    Default Re: Deserialize an array of bytes back to List<List<String>>

    You save an object of type List<List<String>> via ObjectOutputStream.
    You load the file via ObjectInputStream and cast it onto a List<List<String>> object.
    igorland likes this.

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,203
    Rep Power
    12

    Default Re: Deserialize an array of bytes back to List<List<String>>

    Just write it out and read it in. ArrayList is serializable and has its own writeObject and readObject methods. So any required bookkeeping is done for you.

    Regards,
    Jim
    igorland likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    89
    Rep Power
    0

    Default Re: Deserialize an array of bytes back to List<List<String>>

    I did not realize that it is as easy as simply casting the object. I was looking for complex solutions and methods. It works. Thanks a lot, everyone!

Similar Threads

  1. arrg! back again with a List[].add() problem.
    By NotTooOld in forum New To Java
    Replies: 2
    Last Post: 04-23-2013, 01:44 PM
  2. Replies: 4
    Last Post: 01-29-2013, 06:40 AM
  3. List of Objects to String array
    By padam in forum New To Java
    Replies: 3
    Last Post: 05-17-2012, 04:35 PM
  4. Replies: 8
    Last Post: 11-04-2011, 08:30 PM
  5. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 05:37 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •