Java Utililty Methods Serialize

List of utility methods to do Serialize

Description

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

Method

voidserializeObjectToFile(Object obj, File f)
serialize Object To File
FileOutputStream fos = new FileOutputStream(f);
try {
    BufferedOutputStream bos = new BufferedOutputStream(fos, 1024 * 128);
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(obj);
    oos.close();
} finally {
    fos.close();
...
voidserializeObjectToFile(Object object, String filename)
Serializes object into filename .
FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
    fos = new FileOutputStream(filename);
    out = new ObjectOutputStream(fos);
    out.writeObject(object);
    out.close();
} catch (IOException ex) {
...
byte[]serializer(Object object)
serializer
ObjectOutputStream objectOutputStream = null;
ByteArrayOutputStream byteArrayOutputStream;
try {
    byteArrayOutputStream = new ByteArrayOutputStream();
    objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
    objectOutputStream.writeObject(object);
    objectOutputStream.flush();
    return byteArrayOutputStream.toByteArray();
...
byte[]serializeSafe(T obj)
serialize Safe
ByteArrayOutputStream baos = null;
try {
    baos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    try {
        out = new ObjectOutputStream(baos);
        obj.writeExternal(out);
        out.flush();
...
intserializeShort(short value, byte[] outbuf, int offset)
Serialize a short, but do not convert it to little endian in the process
outbuf[offset++] = (byte) (value >> 8);
outbuf[offset++] = (byte) (value);
return offset;
voidserializeString(File file, String string)
Dump a String to a text file.
serializeString(file, string, null);
StringserializeToBase64(Serializable object)
serialize To Base
if (object == null) {
    return "";
ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
ObjectOutputStream objectStream = null;
try {
    objectStream = new ObjectOutputStream(byteArrayStream);
    objectStream.writeObject(object);
...
voidserializeToFile(Object obj, String fileName)
serialize To File
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(fileName)));
out.writeObject(obj);
out.close();
voidserializeToFile(Object object, File file)
Utility method to serialize an object to the given File.
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
oos.writeObject(object);
oos.close();
voidserializeToFile(Serializable s, String path)
serialize To File
FileOutputStream fileOut = new FileOutputStream(path);
serialize(s, fileOut);
fileOut.close();