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

歡迎訪問 生活随笔!

生活随笔

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

数据库

大量json数据解析OOM 存储数据库 assets下的json压缩文件解压

發布時間:2023/12/20 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 大量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压缩文件解压的全部內容,希望文章能夠幫你解決所遇到的問題。

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