Java 怎么序列化 List

2019年5月30日19:07:54 评论 83

1.简介

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

2.代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */
public class SerializeList {
	public static <T> void serializList(List<T> collections, String path) {
		File file = new File(path);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		ObjectOutputStream objectOutputStream = null;
		try {
			OutputStream outputStream = new FileOutputStream(file, true);
			objectOutputStream = new ObjectOutputStream(outputStream);
			if (file.length() < 1) {
				objectOutputStream = new ObjectOutputStream(outputStream);
			} else {
				objectOutputStream = new MyObjectOutputStream(outputStream);
			}
			for (T collection : collections) {
				objectOutputStream.writeObject(collection);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				objectOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
}

3.总结

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

发表评论

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