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

voidserialize(Serializable s, File dir, String fileName)
serialize
new ObjectOutputStream(new FileOutputStream(dir.getPath() + File.separator + fileName)).writeObject(s);
byte[]serialize(Serializable serializable)
serialize
try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(serializable);
    oos.close();
    return baos.toByteArray();
} catch (IOException e) {
    throw new RuntimeException("Failed to serialize!", e);
...
byte[]serialize(Serializable... objects)
This method takes the given serializable arguments, serializes them into a byte[] and passes it back to the caller.
try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    for (Serializable temp : objects)
        oos.writeObject(temp);
    oos.close();
    return baos.toByteArray();
} catch (IOException ioex) {
...
voidserialize(String filename, T obj)
Serialize an object to file.
FileOutputStream fileOut = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(obj);
voidserialize(String path, String name, Object obj)
serialize
checkPath(path);
if (!(obj instanceof Serializable)) {
    return;
ObjectOutputStream oos = null;
try {
    oos = new ObjectOutputStream(new FileOutputStream(path + name));
    oos.writeObject(obj);
...
byte[]serialize(T obj)
serialize
ObjectOutputStream oos = null;
ByteArrayOutputStream os = null;
try {
    os = new ByteArrayOutputStream();
    oos = new ObjectOutputStream(os);
    oos.writeObject(obj);
    return os.toByteArray();
} catch (IOException e) {
...
byte[]serialize(T obj)
serialize
try {
    ByteArrayOutputStream baos = null;
    try {
        baos = new ByteArrayOutputStream();
        ObjectOutput out = null;
        try {
            out = new ObjectOutputStream(baos);
            out.writeObject(obj);
...
byte[]serialize(T object)
serialize
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(object);
oos.close();
return baos.toByteArray();
byte[]serialize(T object)
serialize
if (null == object)
    return null;
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
byte[] bytes = null;
try {
    baos = new ByteArrayOutputStream();
    oos = new ObjectOutputStream(baos);
...
byte[]serialize(T object)
Serialize an object into a byte array.
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(object);
objectOutputStream.close();
return byteArrayOutputStream.toByteArray();