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> ?

Community
  • 1
  • 1
iSun
  • 1,714
  • 6
  • 28
  • 57
  • 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. – Dunes Dec 15 '11 at 17:19
  • possible duplicate of [What is character encoding and why should I bother with it](http://stackoverflow.com/questions/10611455/what-is-character-encoding-and-why-should-i-bother-with-it) – Raedwald Apr 10 '15 at 12:09

4 Answers4

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();
        }
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
soulcheck
  • 36,297
  • 6
  • 91
  • 90
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);
}
Brian
  • 6,391
  • 3
  • 33
  • 49
  • Np. Best ask that one as a new question Ali, with more examples of exactly what you're looking to achieve – Brian Dec 15 '11 at 17:28
  • @Ali: Do you mean String[] to ArrayList - if so, you can use Arrays.asList – Sid Malani Dec 15 '11 at 17:38
  • @SidMalani : No, Just String to ArrayList. – iSun Dec 15 '11 at 17:43
  • 1
    @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. – Sid Malani Dec 15 '11 at 17:48
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);
Kashif Khan
  • 2,615
  • 14
  • 14
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.

alf
  • 8,377
  • 24
  • 45