日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 文件的存储和加载

發布時間:2024/4/14 Android 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 文件的存储和加载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android 文件的存儲和加載,主要用于請求網絡中json文件的緩存,引入了一個簡單的過期時間,供大家參考學習!

?

文件存儲

1 private void saveLocal(String json, int index) { 2 3 BufferedWriter bw = null; 4 try { 5 File dir=FileUtils.getCacheDir(); 6 //在第一行寫一個過期時間 7 File file = new File(dir, getKey()+"_" + index); // /mnt/sdcard/googlePlay/cache/home_0 8 FileWriter fw = new FileWriter(file); 9 bw = new BufferedWriter(fw); 10 bw.write(System.currentTimeMillis() + 1000 * 100 + ""); 11 bw.newLine();// 換行 12 bw.write(json);// 把整個json文件保存起來 13 bw.flush(); 14 bw.close(); 15 } catch (Exception e) { 16 e.printStackTrace(); 17 }finally{ 18 IOUtils.closeQuietly(bw); 19 } 20 }

文件加載

1 private String loadLocal(int index) { 2 // 如果發現文件已經過期了 就不要再去復用緩存了 3 File dir=FileUtils.getCacheDir();// 獲取緩存所在的文件夾 4 File file = new File(dir, getKey()+"_" + index); 5 try { 6 FileReader fr=new FileReader(file); 7 BufferedReader br=new BufferedReader(fr); 8 long outOfDate = Long.parseLong(br.readLine()); 9 if(System.currentTimeMillis()>outOfDate){ 10 return null; 11 }else{ 12 String str=null; 13 StringWriter sw=new StringWriter(); 14 while((str=br.readLine())!=null){ 15 16 sw.write(str); 17 } 18 return sw.toString(); 19 } 20 21 } catch (Exception e) { 22 e.printStackTrace(); 23 return null; 24 } 25 }

?

轉載于:https://www.cnblogs.com/lude313/p/4796970.html

總結

以上是生活随笔為你收集整理的Android 文件的存储和加载的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。