Java 怎么反序列化 List

2019年5月30日19:09:38Java 怎么反序列化 List已关闭评论 502 590字阅读1分58秒

1.简介

Java 怎么反序列化 List?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */
public class UnserializeList {
	public static <T> List<T> unserializList(String path) {
		File file = new File(path);
		List<T> collections = new ArrayList<T>();
		ObjectInputStream objectInputStream = null;
		try {
			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;
	}
}
编程十万个怎么办