4

I found a question here on SO: Convert ArrayList<String> to byte []

It is about converting ArrayList<String> to byte[].

Now is it possible to convert byte[] to ArrayList<String> ?

2
  • 1
    Why would you turn something into an array of bytes if you couldn't convert it back? And I don't know why you've accepted the answer you did. As it will not produce a list of strings equal to that which was used to create the byte array. Commented Dec 15, 2011 at 17:19
  • possible duplicate of What is character encoding and why should I bother with it Commented Apr 10, 2015 at 12:09

4 Answers 4

8

Looks like nobody read the original question :)

If you used the method from the first answer to serialize each string separately, doing exactly the opposite will yield the required result:

    ByteArrayInputStream bais = new ByteArrayInputStream(byte[] yourData);
    ObjectInputStream ois = new ObjectInputStream(bais);
    ArrayList<String> al = new ArrayList<String>();
    try {
        Object obj = null;

        while ((obj = ois.readObject()) != null) {
            al.add((String) obj);
        }
    } catch (EOFException ex) { //This exception will be caught when EOF is reached
        System.out.println("End of file reached.");
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        //Close the ObjectInputStream
        try {
            if (ois != null) {
                ois.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

If your byte[] contains the ArrayList itself, you can do:

    ByteArrayInputStream bais = new ByteArrayInputStream(byte[] yourData);
    ObjectInputStream ois = new ObjectInputStream(bais);
    try {
        ArrayList<String> arrayList = ( ArrayList<String>) ois.readObject();
        ois.close();
    } catch (EOFException ex) { //This exception will be caught when EOF is reached
        System.out.println("End of file reached.");
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        //Close the ObjectInputStream
        try {
            if (ois!= null) {
                ois.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

6

Something like this should suffice, forgive any compile typos I've just rattled it out here:

for(int i = 0; i < allbytes.length; i++)
{
    String str = new String(allbytes[i]);
    myarraylist.add(str);
}

4 Comments

Np. Best ask that one as a new question Ali, with more examples of exactly what you're looking to achieve
@Ali: Do you mean String[] to ArrayList<String> - if so, you can use Arrays.asList
@SidMalani : No, Just String to ArrayList<String>.
@Ali Arrays.asList(string.split("")); I am assuming you want to split for each character. It I think will introduce a space in first element which you can easily remove.
3

yeah its possible, take each item from byte array and convert to string, then add to arraylist

String str = new String(byte[i]);
arraylist.add(str);

Comments

1

it depends very much on the semantics you expect from such a method. The easiest way would be, new String(bytes, "US-ASCII")—and then split it into the details you want.

There are obviously some problems:

  1. How can we be sure it's "US-ASCII" and not "UTF8" or, say, "Cp1251"?
  2. What is the string delimiter?
  3. What if we want one of the strings to contain a delimiter?

And so on and so forth. But the easiest way is indeed to call String constructor—it'll be enough to get you started.

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.