Android Object Serialization writeObjectToFile(Object obj, String filePath)

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

Description

write Object To File

Declaration

public static void writeObjectToFile(Object obj, String filePath) 

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 {//  www. j  a v  a2  s  .co m
            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. stringToObject(String s)
  2. getBytes(Serializable obj)
  3. deserialize(final File file, final long serialTtl)
  4. serialize(final File file, final Object o)
  5. saveObject(Context context, String fileName, Object obj)
  6. writeObjectToFile(Object obj, String filePath, boolean append)
  7. writeQueueToFile(Queue list, String filePath)
  8. ObjectToByte(Serializable obj)
  9. obj2Bytes(Object obj)