分享個 之前寫好的 android 文件流緩存類,專門處理 ArrayList、bean。


轉載麻煩聲明出處:http://www.cnblogs.com/linguanh/

目錄:

  1,前序

      2,作用

   3,特點

       4,代碼

 

 

1,前序

   在開發過程中,client 和 server 數據交流一般用到 json 格式傳輸數據。緩存機制,是任何一個優秀的 app 都必須存在的,android 的緩存數據的方法很多,無論是文本還是圖像,我這里要講的是我自己 編寫 並 一直使用的, DataInfoCache 類。

 

2,本類作用

   專門存儲 ArrayList 種子數據,我舉個 例子: List<UserInfo> mInfoBean = new ArrayList<>();  這里的 bean 種子是 UserInfo 類,處理這里信息,我們一般是在接受完 server 的數據,例如 json 后,解析完 json,再通過 setXXX() 函數來存入,再用 getXXX() 獲取。 在有網絡的情況下,我們可以輕易 獲取數據,沒有網絡呢? 那么我們就應該去獲取緩存的。 那么問題就來了,要獲取緩存,需要滿足條件:

     有網絡時獲取數據 ---> 先保存數據到本地,作為緩存;

     沒網絡時             ---> 讀取本地緩存;

     目前到這里來說,本類的作用和 一般的 緩存類 沒什么差別,OK,請看第三點。

 

3,特點

     1,代碼量少,通俗易懂,連帶注釋,不到 130 行;

     2,存儲時,直接 存儲 List,讀取時,直接讀出 List , 直接用,無需 再解析。    下面舉個例子

          List<UserInfo> mInfoBean = new ArrayList<>();

          saveListCache(mInfoBean,"我的緩存");   /**  存儲 */

          mInfoBean = loadListCache("我的緩存"); /** 獲取 */

     有沒有覺得很快,我既不是 存儲 json,也不是存儲 文本,如果存儲的是 json,讀取的時候,你就還需要解析了,文本也是。

 

4,整頁代碼

     內部注釋豐富,相信你決定看得懂,而且,代碼量真心少啊,功能強大。

  1 package cn.share.bananacloud.common;
  2 
  3 import android.content.Context;
  4 import android.util.Log;
  5 
  6 import java.io.File;
  7 import java.io.FileInputStream;
  8 import java.io.FileOutputStream;
  9 import java.io.ObjectInputStream;
 10 import java.io.ObjectOutputStream;
 11 import java.util.ArrayList;
 12 
 13 /**
 14  * Created by Administrator(林冠宏) on 2016/1/20.
 15  *
 16  * 本類 采用泛型 廣泛接受 ArrayList 直接做緩存,即是開發中經常用到的 bean;
 17  *
 18  * 使用注意: 傳進來的 ArrayList 所綁定的 種子,一定要是 已經繼承 Serializable 接口的;
 19  * 
 20  * 使用文本流做緩存。
 21  *
 22  */
 23 
 24 public class DataInfoCache {
 25 
 26     /** 定義一些你項目里面的 緩存文件名字 ,自定義,不要也沒關系,調用函數再傳入也行 */
 27 
 28     public static String  QzInfo = "Qz_List_Info";
 29     public static String  CyInfo = "Cy_List_Info";
 30     private static String DataCache = "Data_Cache_File";
 31 
 32     /**
 33      * 保存 一組 數據
 34      * @param ctx  上下文
 35      * @param data 種子
 36      * @param cacheName 緩存文件名
 37      */
 38     public static<T> void saveListCache(Context ctx, ArrayList<T> data,String cacheName) {
 39         new DataCache<T>().saveGlobal(ctx, data, cacheName);
 40     }
 41 
 42     /**
 43      * 直接根據 緩存文件名獲取
 44      * */
 45     public static<T> ArrayList<T> loadListCache(Context ctx,String cacheName) {
 46         return new DataCache<T>().loadGlobal(ctx,cacheName);
 47     }
 48 
 49     /**
 50      * 獲取 一組 數據
 51      * @param <T> 數據緩存 save or load
 52      */
 53     static class DataCache<T> {
 54         public void save(Context ctx, ArrayList<T> data, String name) {
 55             save(ctx, data, name, "");
 56         }
 57 
 58         public void saveGlobal(Context ctx, ArrayList<T> data, String name) {
 59             save(ctx, data, name, DataCache);
 60         }
 61 
 62         private void save(Context ctx, ArrayList<T> data, String name,String folder) {
 63             if (ctx == null) {
 64                 return;
 65             }
 66             File file;
 67             if (!folder.isEmpty()) {
 68                 File fileDir = new File(ctx.getFilesDir(), folder);
 69                 if (!fileDir.exists() || !fileDir.isDirectory()) {
 70                     fileDir.mkdir();
 71                 }
 72                 file = new File(fileDir, name);
 73             } else {
 74                 file = new File(ctx.getFilesDir(), name);
 75             }
 76             if (file.exists()) {
 77                 file.delete();
 78             }
 79             Log.d("zzzzz", file.getAbsolutePath());
 80             try {
 81                 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
 82                 oos.writeObject(data);
 83                 oos.close();
 84             } catch (Exception e) {
 85                 e.printStackTrace();
 86             }
 87         }
 88 
 89         public ArrayList<T> load(Context ctx, String name) {
 90             return load(ctx, name, "");
 91         }
 92 
 93         public ArrayList<T> loadGlobal(Context ctx, String name) {
 94             return load(ctx, name, DataCache);
 95         }
 96 
 97         private ArrayList<T> load(Context ctx, String name, String folder) {
 98             ArrayList<T> data = null;
 99 
100             File file;
101             if (!folder.isEmpty()) {
102                 File fileDir = new File(ctx.getFilesDir(), folder);
103                 if (!fileDir.exists() || !fileDir.isDirectory()) {
104                     fileDir.mkdir();
105                 }
106                 file = new File(fileDir, name);
107             } else {
108                 file = new File(ctx.getFilesDir(), name);
109             }
110             Log.d("zzzzz","file "+file.getAbsolutePath());
111             if (file.exists()) {
112                 try {
113                     Log.d("zzzzz","write object");
114                     ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
115                     data = (ArrayList<T>) ois.readObject();
116                     ois.close();
117                 } catch (Exception e) {
118                     Log.d("zzzzz",e.toString());
119                 }
120             }
121             if (data == null) {     /** 如果沒有 */
122                 Log.d("zzzzz","data == null");
123                 data = new ArrayList<T>();
124             }
125             return data;
126         }
127     }
128 }
View Code

 

打完收工,麻煩點個頂。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM