List 컬렉션을 로컬에 저장

5365 단어 #도구류
우선 범용 먼저 Serializable 인터페이스를 실현해야 한다
그리고 다음 도구류를 사용할 수 있습니다.
package com.tools.dbattery.bean;

import java.io.Serializable;

/**
 * Name: ChargeRecordsInfo
 * Author: liuan
 * creatTime:2017-08-02 18:09
 * Email:[email protected]
 */
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:[email protected]
 */
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 getStorageEntities(String fileName) {
        ObjectInputStream objectInputStream = null;
        FileInputStream fileInputStream = null;
        ArrayList savedArrayList = new ArrayList<>();
        try {
            File file = new File(AppContext.getFilesDir().getAbsoluteFile(), fileName);
            fileInputStream = new FileInputStream(file.toString());
            objectInputStream = new ObjectInputStream(fileInputStream);
            savedArrayList = (ArrayList) 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 getStorageEntitiesSpinner(String fileName) {
        ObjectInputStream objectInputStream = null;
        FileInputStream fileInputStream = null;
        ArrayList savedArrayList = new ArrayList<>();
        try {
            File file = new File(AppContext.getFilesDir().getAbsoluteFile(), fileName);
            fileInputStream = new FileInputStream(file.toString());
            objectInputStream = new ObjectInputStream(fileInputStream);
            savedArrayList = (ArrayList) 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;
    }
}

좋은 웹페이지 즐겨찾기