Tag: serialization

  • Android Java Serialization Using Generics

    I have created a class – SerializationManager, it can serialize any objects and write them to files. It can be used to save and restore form states in Android. It is based on Java Generics. What is achieved by this is that you do not need to duplicate serialization code in every class.

    images

    Object must implement Serialized interface. Here form is an object instance of a class. It can be named as just obj.

    package com.alwawee.util;
    
    import android.content.Context;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    
    public class SerializationManager {
    
        //================================================================================
        // Constructor
        //================================================================================
        public SerializationManager(){
    
        }
    
    
        //================================================================================
        // Other
        //================================================================================
        // Reads saved object from a file. Object must implement Serialized interface.
        public T readObjectFromFile(Context context, String className) {
            T form = null;
            try {
                String fileName = className + ".ser";
                FileInputStream fileInputStream = context.openFileInput(fileName);
                ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
                form = (T) objectInputStream.readObject();
                objectInputStream.close();
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            return form;
        }
    
    
        // Serializes object and saves it to a file. File name is created using object type (class name). 
        public void saveObjectToFile(Context context, T form) {
            try {
                String fileName = form.getClass().getSimpleName() + ".ser";
                FileOutputStream fileOutputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
                objectOutputStream.writeObject(form);
                objectOutputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    This is how you can use it. First you declare a SerializationManager in your class and you tell it which class you are going to serialize:

    private SerializationManager manager;
    

    Then in onCreate method you create SerializationManager instance:

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            manager = new SerializationManager();
    

    Writing object to a file:

    // Saving to a file
    manager.saveObjectToFile(this, form);
    

    Reading objects from saved file:

    // Reading form object from a file
    form = manager.readObjectFromFile(this, "CreateResumeForm");
    

    A little joke for reading to the end:
    java generics

  • Serialize an object, save it to a file and read it from a file in Android

    Android interface Serializable is not sufficient to save your objects to the file, it only creates streams.

    This is a complete code with error handling and handling closes of streams. Add it to your class that you want to be able to serialize and deserialize.

    In my case the class name is CreateResumeForm. You should change it to your own class name.

    // Constant with a file name
    public static String fileName = "createResumeForm.ser";
    
    // Serializes an object and saves it to a file
    public void saveToFile(Context context) {
        try {
            FileOutputStream fileOutputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(this);
            objectOutputStream.close();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
    // Creates an object by reading it from a file
    public static CreateResumeForm readFromFile(Context context) {
        CreateResumeForm createResumeForm = null;
        try {
            FileInputStream fileInputStream = context.openFileInput(fileName);
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
            createResumeForm = (CreateResumeForm) objectInputStream.readObject();
            objectInputStream.close();
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return createResumeForm;
    }
    

    Use it like this in your Activity:

    form = CreateResumeForm.readFromFile(this);