List集合存储到本地

首先泛型要先实现Serializable接口

然后就可以使用如下工具类了

package com.tools.dbattery.bean;

import java.io.Serializable;

/**
 * Name: ChargeRecordsInfo
 * Author: liuan
 * creatTime:2017-08-02 18:09
 * Email:1377093782@qq.com
 */
public class ChargeRecordsInfo implements Serializable {

    public String beaginTime;
    public String beaginLevel;
    public String endLevel;
    public String charegTime;
    public int boostTime;
    public int coolerTime;

    public void setBeaginTime(String beaginTime) {
        this.beaginTime = beaginTime;
    }

    public void setBeaginLevel(String beaginLevel) {
        this.beaginLevel = beaginLevel;
    }

    public void setEndLevel(String endLevel) {
        this.endLevel = endLevel;
    }

    public void setCharegTime(String charegTime) {
        this.charegTime = charegTime;
    }

    public void setBoostTime(int boostTime) {
        this.boostTime = boostTime;
    }

    public void setCoolerTime(int coolerTime) {
        this.coolerTime = coolerTime;
    }

    @Override
    public String toString() {
        return "ChargeRecordsInfo{" +
                "beaginTime='" + beaginTime + '\'' +
                ", beaginLevel='" + beaginLevel + '\'' +
                ", endLevel='" + endLevel + '\'' +
                ", charegTime='" + charegTime + '\'' +
                ", boostTime=" + boostTime +
                ", coolerTime=" + coolerTime +
                '}';
    }
}



/**
 * Name: ListSaveUtils
 * Author: liuan
 * creatTime:2017-08-03 15:30
 * Email:1377093782@qq.com
 */
public class ListSaveUtils {
    /**
     * 数据存放在本地
     *
     * @param tArrayList
     */
    public static void saveStorage2SDCard(List tArrayList, String fileName) {

        FileOutputStream fileOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        FileInputStream fileInputStream = null;
        try {
            File file = new File(AppContext.getFilesDir().getAbsoluteFile(), fileName);
            fileOutputStream = new FileOutputStream(file.toString());  //新建一个内容为空的文件
            objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(tArrayList);
        } 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
     */
    public static ArrayList<ChargeRecordsInfo> getStorageEntities(String fileName) {
        ObjectInputStream objectInputStream = null;
        FileInputStream fileInputStream = null;
        ArrayList<ChargeRecordsInfo> savedArrayList = new ArrayList<>();
        try {
            File file = new File(AppContext.getFilesDir().getAbsoluteFile(), fileName);
            fileInputStream = new FileInputStream(file.toString());
            objectInputStream = new ObjectInputStream(fileInputStream);
            savedArrayList = (ArrayList<ChargeRecordsInfo>) 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 savedArrayList;
    }


    /**
     * 获取本地的List数据
     *
     * @return
     */
    public static ArrayList<Integer> getStorageEntitiesSpinner(String fileName) {
        ObjectInputStream objectInputStream = null;
        FileInputStream fileInputStream = null;
        ArrayList<Integer> savedArrayList = new ArrayList<>();
        try {
            File file = new File(AppContext.getFilesDir().getAbsoluteFile(), fileName);
            fileInputStream = new FileInputStream(file.toString());
            objectInputStream = new ObjectInputStream(fileInputStream);
            savedArrayList = (ArrayList<Integer>) 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 savedArrayList;
    }
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值