集册 Java实例教程 序列化列表

序列化列表

欢马劈雪     最近更新时间:2020-01-02 10:19:05

511
序列化列表


import java.io.*;

import java.util.ArrayList;

import java.util.List;
/**
NowJava.com - 时代Java
**/


public class Main{

    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();

            }

        }/** 来自 n o w    j a v a  . c o m**/

        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) {

        
展开阅读全文