Java Object Deserialize from File deserialize(String filename)

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

Description

deserialize

License

Open Source License

Parameter

Parameter Description
T a parameter
filename a parameter

Exception

Parameter Description
IOException an exception

Return

a T object read in the file passed in parameter

Declaration

@SuppressWarnings("unchecked")
public static <T extends Serializable> T deserialize(String filename)
        throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.FileInputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.Serializable;

public class Main {
    /**/*  w  w w  .  j a  v  a2s . c o m*/
     * @param <T>
     * @param filename
     * @return a T object read in the file passed in parameter
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    public static <T extends Serializable> T deserialize(String filename)
            throws IOException {
        ObjectInputStream ois = null;
        T t = null;
        try {
            final FileInputStream fichier = new FileInputStream(filename);
            ois = new ObjectInputStream(fichier);
            t = (T) ois.readObject();

        } catch (final java.io.IOException e) {
            e.printStackTrace();
        } catch (final ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                ois.close();
            }
            if (t == null) {
                throw new IOException(
                        "error when deserialisation (T) object");
            }
        }
        return t;
    }
}

Related

  1. deserialize(File file, Class clazz)
  2. deserialize(final File file)
  3. deserialize(final String path)
  4. deserialize(String filename)
  5. deserialize(String fileName)
  6. Deserialize(String filePath)
  7. deserialize(String filePath)
  8. deserialize(String fName)
  9. deserialize(String fname)