Here you can find the source of getBytes(Object obj)
Parameter | Description |
---|---|
obj | the obj |
Parameter | Description |
---|---|
IOException | Signals that an I/O exception has occurred. |
public static byte[] getBytes(Object obj) throws IOException
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class Main { /**//from ww w . ja va 2 s. c o m * Gets the bytes. * * @param obj the obj * @return the bytes * @throws IOException Signals that an I/O exception has occurred. */ public static byte[] getBytes(Object obj) throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bout); out.writeObject(obj); out.flush(); byte[] bytes = bout.toByteArray(); bout.close(); out.close(); return bytes; } }