• 文件缓存(配合JSON数组)


    1.   写入缓存:建立文件夹,把list集合里面的数组转换为JSON数组,存入文件夹
    2.  读取缓存:把JSON数组从文件夹里面读取出来,然后放入list集合,返回list集合

    private final static File filefolder=new File("/sdcard/myData");
    private final static File filename=new File("/sdcard/myData/tem.txt"); public static boolean writeCache(List<Data> list) { if(!filefolder.exists()) filefolder.mkdirs(); try { JSONArray array=new JSONArray(); for(int i=0;i<list.size();i++) { Data data=list.get(i); JSONObject ob=new JSONObject(); ob.put("name", data.getName()); ob.put("reason", data.getReason()); array.put(ob); } FileWriter fw=new FileWriter(filename); fw.write(array.toString()); fw.close(); } catch(Exception e) { e.printStackTrace(); return false; } return true; } public static List<Data> readCache() throws JSONException,IOException { if(!filefolder.exists()) filefolder.mkdir(); List<Data> list=new ArrayList<Data>(); if(filename.exists()) { FileInputStream in=new FileInputStream(filename); String line=null; StringBuffer sb=new StringBuffer(""); BufferedReader br=new BufferedReader(new InputStreamReader(in)); while((line=br.readLine())!=null) sb.append(line); br.close(); in.close(); JSONArray array=new JSONArray(sb.toString()); for(int i=0;i<array.length();i++) { JSONObject ob=new JSONObject(); ob=array.getJSONObject(i); Data data=new Data(); data.setName(ob.getString("name")); data.setReason(ob.getString("reason")); list.add(data); } } return list; }
    复制代码
  • 相关阅读:
    清北学堂模拟赛d6t1 角谷猜想
    清北学堂模拟赛d4t1 a
    清北学堂模拟赛d3t6 c
    清北学堂模拟赛d3t5 c
    清北学堂模拟赛d3t4 a
    清北学堂模拟赛d3t3 c
    清北学堂模拟赛d3t1 a
    清北学堂模拟赛d2t3 逆序对(pair)
    Android(java)学习笔记176: 远程服务的应用场景(移动支付案例)
    Android(java)学习笔记175:Android进程间通讯(IPC)之AIDL
  • 原文地址:https://www.cnblogs.com/mrray/p/6169844.html
Copyright © 2020-2023  润新知