大量json数据解析OOM 存储数据库 assets下的json压缩文件解压
生活随笔
收集整理的這篇文章主要介紹了
大量json数据解析OOM 存储数据库 assets下的json压缩文件解压
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
項目背景:assets包下有個json壓縮文件,要求對該文件進行解壓,再對解壓后的json文件進行解析,并最終將數據存入數據庫中。json文件包含30萬條數據,文件大小是180M,由于數據量大,采用了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];// 讀取(字節)字節到緩沖區while ((len = inZip.read(buffer)) != -1) {// 從緩沖區(0)位置寫入(字節)字節out.write(buffer, 0, len);out.flush();}out.close();}}}inZip.close();} }調用解壓:
ZipUtils.UnZipAssetsFolder(this, "json.zip", "/storage/emulated/0/json");解析工具類中的具體實現方法--直接調用即可:
/*** 從文件中讀取信息,并轉換為相應對象** @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;}?
截止到這里,解壓和解析的工作就結束了,剩下的寫入數據庫操作,下次再更......👌
總結
以上是生活随笔為你收集整理的大量json数据解析OOM 存储数据库 assets下的json压缩文件解压的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小巫随笔12(致小巫逝去的童年),202
- 下一篇: linux cmake编译源码,linu