Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import java.io.FileOutputStream;

import java.io.ObjectOutputStream;

public class Main {

    public static boolean saveObject(@NonNull Context context, Object obj, String fileName) {
        if (obj == null || TextUtils.isEmpty(fileName)) {
            return false;
        }

        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(obj);
            oos.flush();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (fos != null)
                    fos.close();
                if (oos != null)
                    oos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}