将巨大的json对象写入文件

时间:2019-07-16 16:39:46

标签: java android json memory

所以我有一个巨大的JSONObject,我需要将其写入文件,现在我的代码可以在90%的设备上正常运行,问题出在诸如Amazon Fire TV之类的低内存设备上,应用程序崩溃并显示错误“ java.lang.OutOfMemoryError”。 我想知道还有另一种更内存友好的方式将json对象写入文件吗?

那是我的代码:

    try{
        Writer output = null;
        if(jsonFile.isDirectory()){
            jsonFile.delete();
        }
        if(!jsonFile.exists()){
            jsonFile.createNewFile();
        }
        output = new BufferedWriter(new FileWriter(jsonFile));
        output.write(mainObject.toString());
        output.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

0 个答案:

没有答案
相关问题