Java Utililty Methods Object Serialize

List of utility methods to do Object Serialize

Description

The list of methods to do Object Serialize are organized into topic(s).

Method

byte[]serialize(Object obj)
Serialize the object to a byte array.
try {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(out);
    os.writeObject(obj);
    return out.toByteArray();
} catch (Throwable e) {
    throw new RuntimeException(e);
byte[]serialize(Object obj)
serialize
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ObjectOutputStream out = new ObjectOutputStream(baos)) {
    out.writeObject(obj);
    return baos.toByteArray();
} finally {
    baos.close();
byte[]serialize(Object obj)
serialize
ByteArrayOutputStream b = new ByteArrayOutputStream();
ObjectOutputStream o = new ObjectOutputStream(b);
o.writeObject(obj);
return b.toByteArray();
byte[]serialize(Object obj)
serialize
try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(obj);
    oos.close();
    return baos.toByteArray();
} catch (IOException e) {
    throw new RuntimeException("error serializing " + obj, e);
...
byte[]serialize(Object obj)
Serializes an object into a byte array.
if (obj == null) {
    return null;
try (ByteArrayOutputStream b = new ByteArrayOutputStream()) {
    try (ObjectOutputStream o = new ObjectOutputStream(b)) {
        o.writeObject(obj);
    return b.toByteArray();
...
byte[]serialize(Object obj)
Serializes a serializable object into a byte array.
ByteArrayOutputStream out = new ByteArrayOutputStream();
serialize(out, obj);
return out.toByteArray();
byte[]serialize(Object obj)
serialize
ByteArrayOutputStream baos = null;
ObjectOutputStream os = null;
try {
    baos = new ByteArrayOutputStream(512);
    os = new ObjectOutputStream(baos);
} finally {
    if (baos != null) {
        baos.close();
...
voidserialize(Object obj, String file)
serialize
try {
    int lastDash = Math.max(file.lastIndexOf('\\'), file.lastIndexOf('/'));
    if (lastDash != -1)
        new File(file.substring(0, lastDash)).mkdirs();
    ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
    out.writeObject(obj);
    out.flush();
    out.close();
...
voidserialize(Object obj, String fileName)
serialize
FileOutputStream fos = new FileOutputStream(FILE_DIR + fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(obj);
fos.close();
voidserialize(Object obj, String fileName)
serialize the given object and save it to given file
FileOutputStream fos = new FileOutputStream(fileName);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.close();