Java Utililty Methods Object Save

List of utility methods to do Object Save

Description

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

Method

voidwriteObjectToFile(Object oObject, File destDir, String filename)
write Object To File
File dest = new File(destDir, filename);
if (dest.exists())
    dest.delete();
OutputStream outStream = null;
try {
    outStream = new BufferedOutputStream(new FileOutputStream(dest));
    outStream.write((byte[]) oObject);
} finally {
...
voidwriteObjectToFile(String dirname, String filename, Object obj)
Write an object to a file.
ObjectOutputStream oos = null;
try {
    oos = new ObjectOutputStream(
            new BufferedOutputStream(new FileOutputStream(new File(dirname, filename))));
    oos.writeObject(obj);
    oos.flush();
} catch (IOException e) {
    System.err.println("write: " + e);
...
booleanwriteObjectToFile(String fileName, Object obj)
write Object To File
try {
    File f = new File(fileName);
    f.setWritable(true);
    FileOutputStream fout = new FileOutputStream(f);
    ObjectOutputStream oo = new ObjectOutputStream(fout);
    oo.writeObject(obj);
    fout.close();
    return true;
...
voidwriteObjectToFile(String filename, Serializable s)
write Object To File
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename));
oos.writeObject(s);
oos.close();
voidwriteObjectToFile(String filename, T object)
write the object to a json file
ObjectMapper mapper = new ObjectMapper();
File file = new File(filename);
try {
    mapper.writeValue(file, object);
} catch (IOException e) {
    System.err.println("error write object as json file: " + object + " for " + e);
voidwriteObjectToFile(String outputFileName, Object toWrite, boolean append)
Writes an object, using its 'toString' method, to the named file.
Writer resultWriter;
try {
    resultWriter = new FileWriter(outputFileName, append);
} catch (IOException e) {
    throw new IllegalStateException("Unable to open the output file '" + outputFileName + "' for writing.",
            e);
try {
...
booleanwriteObjectToFile(T object, File fileHandle)
Write a serializable object to a file
try {
    FileOutputStream fos = new FileOutputStream(fileHandle);
    ObjectOutputStream output = new ObjectOutputStream(fos);
    output.writeObject(object);
    output.close();
    fos.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
...
FilewriteObjectToFileNoExceptions(Object o, String filename)
Write object to a file with the specified name.
File file = null;
ObjectOutputStream oos = null;
try {
    file = new File(filename);
    oos = new ObjectOutputStream(
            new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(file))));
    oos.writeObject(o);
    oos.close();
...