Android Object Serialization readObjectFromFile(String filename)

Here you can find the source of readObjectFromFile(String filename)

Description

Read a serialized object from a file

Parameter

Parameter Description
filename the file to read from

Return

the object read from the file or null if an error occurred

Declaration

public static Object readObjectFromFile(String filename) 

Method Source Code

//package com.java2s;
import java.io.BufferedInputStream;

import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;

public class Main {
    /**/*from ww w . j av a2 s. c o  m*/
     * Read a serialized object from a file
     * @param filename the file to read from
     * @return the object read from the file or null if an error occurred
     */
    public static Object readObjectFromFile(String filename) {
        //deserialize the the file

        try {
            //use buffering
            InputStream file = new FileInputStream(filename);
            InputStream buffer = new BufferedInputStream(file);
            ObjectInput input = new ObjectInputStream(buffer);
            try {
                //display its data
                return input.readObject();
            } finally {
                input.close();
            }
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}

Related

  1. loadObjectFromFile(String filename)
  2. readObjectFromFile(File fileLocation)
  3. saveObjectAsFile(String filename, Object object)
  4. writeObjectToFile(File fileLocation, Object obj)
  5. writeObjectToFile(Serializable obj, String filename)
  6. objectToString(Object o)