Skip to content

Instantly share code, notes, and snippets.

@zhaohangbo
Created September 23, 2016 13:12
Show Gist options
  • Select an option

  • Save zhaohangbo/f2f36b68a57b2279142ecaebaa4fdbb1 to your computer and use it in GitHub Desktop.

Select an option

Save zhaohangbo/f2f36b68a57b2279142ecaebaa4fdbb1 to your computer and use it in GitHub Desktop.

Revisions

  1. zhaohangbo created this gist Sep 23, 2016.
    17 changes: 17 additions & 0 deletions Java Serializable Object to Byte Array
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    http://stackoverflow.com/questions/2836646/java-serializable-object-to-byte-array

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream out = null;
    try {
    out = new ObjectOutputStream(bos);
    out.writeObject(yourObject);
    out.flush();
    byte[] yourBytes = bos.toByteArray();
    ...
    } finally {
    try {
    bos.close();
    } catch (IOException ex) {
    // ignore close exception
    }
    }