So i have a huge JSONObject that I need to write to a file, right now my code work perfectly on 90% of the devices, the problem is on low memory devices such as Amazon Fire TV the app crashes with an error "java.lang.OutOfMemoryError". I wonder is there another more memory friendly way to write that json object to file?
That's my code:
    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();
    }
 
    