unserialize List - Java Collection Framework

Java examples for Collection Framework:List

Description

unserialize List

Demo Code


//package com.java2s;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static <T> List<T> unserializList(String path) {
        File file = new File(path);
        List<T> collections = new ArrayList<T>();
        ObjectInputStream objectInputStream = null;
        try {//from  w ww .  j av a  2s.c  o  m
            InputStream inputStream = new FileInputStream(file);
            objectInputStream = new ObjectInputStream(inputStream);
            while (true) {
                try {
                    T t = (T) objectInputStream.readObject();
                    collections.add(t);
                } catch (EOFException e) {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                objectInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return collections;
    }
}

Related Tutorials