android - How to append items to JSON file without parsing and rewriting -
i generating observation objects , want add them json file. way i'm doing now, i'm parsing existing file , converting json object, adding new observation instance retrieved collection, parsing json , re-writing file.
here's code:
final file file = getfile(); final observationset observationset = retrievepreviousobservations(file); observationset.getobservations().add(observation[0]); final outputstream os = new fileoutputstream(file); os.write(gsonhelper.tojson(observationset).getbytes()); os.close();
...
private observationset retrievepreviousobservations(final file file) { observationset observationset = new observationset(); try { final bufferedreader br = new bufferedreader(new filereader(file)); observationset = gsonhelper.getgson().fromjson(br, observationset.class); } catch (filenotfoundexception e) { log.w(tag, "no previous file found, first entry", e); } return observationset; }
the problem is, observation objects quite big , after several tens of entries, appending process takes long time (generally more 10 seconds 20+ items in collection).
is there way append existing json file without having parse existing one, adding object, parsing , rewriting file?
Comments
Post a Comment