简体   繁体   中英

Convert byte[] to ArrayList<String>

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

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();
        }
    }
}

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);
}

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM