Java 怎么反序列化 List

2019年5月30日19:09:38 评论 135

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;
	}
}

3.总结

示例代码演示了 Java 怎么反序列化 List,具体步骤参见示例代码。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: