List存取

package util;

import android.content.Context;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import bean.Book;

/**
 * Created on 2018/6/24 0024.
 *
 * @author 郑少鹏
 * @desc List
 */
public class ListUtils {
    /**
     * 存List(data->data->包名->files)
     *
     * @param context  上下文
     * @param list     集合
     * @param fileName 文件名
     */
    public static void saveListToDataData(Context context, List list, String fileName) {
        FileOutputStream fileOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        try {
            File file = new File(context.getFilesDir().getAbsoluteFile(), fileName);
            // 建空文件
            fileOutputStream = new FileOutputStream(file.toString());
            objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (objectOutputStream != null) {
            try {
                objectOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fileOutputStream != null) {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 本地List
     *
     * @return 本地List
     */
    public static List<Book.DataBean> getListFromDataData(Context context, String fileName) {
        ObjectInputStream objectInputStream = null;
        FileInputStream fileInputStream = null;
        List<Book.DataBean> saveList = new ArrayList<>();
        try {
            File file = new File(context.getFilesDir().getAbsoluteFile(), fileName);
            fileInputStream = new FileInputStream(file.toString());
            objectInputStream = new ObjectInputStream(fileInputStream);
            saveList = (List<Book.DataBean>) objectInputStream.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (objectInputStream != null) {
                    objectInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return saveList;
    }
}

猜你喜欢

转载自blog.csdn.net/zsp_android_com/article/details/80796470