大量json数据解析OOM 存储数据库 assets下的json压缩文件解压
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                大量json数据解析OOM 存储数据库  assets下的json压缩文件解压
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                項(xiàng)目背景:assets包下有個json壓縮文件,要求對該文件進(jìn)行解壓,再對解壓后的json文件進(jìn)行解析,并最終將數(shù)據(jù)存入數(shù)據(jù)庫中。json文件包含30萬條數(shù)據(jù),文件大小是180M,由于數(shù)據(jù)量大,采用了gson.fromJson(reader, bean) 方式。
解壓工具類:
public class ZipUtils {public static final String TAG = ZipUtils.class.getSimpleName();/*** 解壓assets目錄下的zip到指定的路徑** @param zipFileString ZIP的名稱,壓縮包的名稱:json.zip* @param outPathString 解壓后文件存放的路徑* @throws Exception*/public static void UnZipAssetsFolder(Context context, String zipFileString, StringoutPathString) throws Exception {ZipInputStream inZip = new ZipInputStream(context.getAssets().open(zipFileString));ZipEntry zipEntry;String szName = "";while ((zipEntry = inZip.getNextEntry()) != null) {szName = zipEntry.getName();if (zipEntry.isDirectory()) {//獲取部件的文件夾名szName = szName.substring(0, szName.length() - 1);File folder = new File(outPathString + File.separator + szName);folder.mkdirs();} else {RceLog.e(TAG, outPathString + File.separator + szName);File file = new File(outPathString + File.separator + szName);if (!file.exists()) {RceLog.e(TAG, "Create the file:" + outPathString + File.separator + szName);file.getParentFile().mkdirs();file.createNewFile();// 獲取文件的輸出流FileOutputStream out = new FileOutputStream(file);int len;byte[] buffer = new byte[1024];// 讀取(字節(jié))字節(jié)到緩沖區(qū)while ((len = inZip.read(buffer)) != -1) {// 從緩沖區(qū)(0)位置寫入(字節(jié))字節(jié)out.write(buffer, 0, len);out.flush();}out.close();}}}inZip.close();} }調(diào)用解壓:
ZipUtils.UnZipAssetsFolder(this, "json.zip", "/storage/emulated/0/json");解析工具類中的具體實(shí)現(xiàn)方法--直接調(diào)用即可:
/*** 從文件中讀取信息,并轉(zhuǎn)換為相應(yīng)對象** @return*/public static <T> T readFileDto(Type bean, String filePath) {File file = new File(filePath);if (!file.exists()) {return null;}T dto = null;try {Reader reader = new FileReader(filePath);Gson gson = new Gson();dto = gson.fromJson(reader, bean);} catch (FileNotFoundException e) {e.printStackTrace();}return dto;}?
截止到這里,解壓和解析的工作就結(jié)束了,剩下的寫入數(shù)據(jù)庫操作,下次再更......👌
總結(jié)
以上是生活随笔為你收集整理的大量json数据解析OOM 存储数据库 assets下的json压缩文件解压的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 小巫随笔12(致小巫逝去的童年),202
- 下一篇: WSL嵌入式开发系列教程 4 —— 安装
