Android Object Serialization writeObjectToFile(Object obj, String filePath, boolean append)

Here you can find the source of writeObjectToFile(Object obj, String filePath, boolean append)

Description

write Object To File

Declaration

public static void writeObjectToFile(Object obj, String filePath,
            boolean append) 

Method Source Code

//package com.java2s;

import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.io.ObjectOutputStream;

public class Main {
    public static void writeObjectToFile(Object obj, String filePath,
            boolean append) {
        if (obj == null || filePath == null || filePath.length() == 0)
            return;
        try {//from   ww  w  .  j a va2  s  .com
            File file = new File(filePath);
            if (!file.exists()) {
                File parent = file.getParentFile();
                if (!parent.exists())
                    parent.mkdirs();
                boolean isSuc = file.createNewFile();
                System.out.println("isSuc" + isSuc);
            }
            ObjectOutputStream oos = new ObjectOutputStream(
                    new FileOutputStream(file, append));
            oos.writeObject(obj);
            oos.flush();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void writeObjectToFile(Object obj, String filePath) {
        writeObjectToFile(obj, filePath, false);
    }
}

Related

  1. getBytes(Serializable obj)
  2. deserialize(final File file, final long serialTtl)
  3. serialize(final File file, final Object o)
  4. saveObject(Context context, String fileName, Object obj)
  5. writeObjectToFile(Object obj, String filePath)
  6. writeQueueToFile(Queue list, String filePath)
  7. ObjectToByte(Serializable obj)
  8. obj2Bytes(Object obj)
  9. obj2Bytes(Object obj)