1

I'm trying some stuff in Android at the moment, and I'm trying to save some data to JSON using GSON and also append new data to the same file.

Let's say that this is the first time the code is being run, and the data is being saved to a file called test.json:

Gson gson = new GsonBuilder().setPrettyPrinting().create();
ArrayList<GSONObject> objects = new ArrayList<GSONObject>();

GSONObject obj = new GSONObject("sth", 1, 1345939200000);
objects.add(obj);

String json = gson.toJson(objects);

File file = new File(filePath + fileName);
FileWriter fileWriter = new FileWriter(file, true);

fileWriter.append(json);
fileWriter.close();

Now I have this saved to the file:

[ {
    "something": "sth",
    "id": 1,
    "time": 1345939200000
  } ]

Later i want to run the app again, but then append some new data to the same file so that it looks like this:

[ {
    "sth": "sth",
    "id": 1,
    "time": 1345939200000
  }, 
 {
    "sth": "sth2",
    "id": 2,
    "time": 1345939500000
  } ]

I am trying to save the amount of data and memory used in this process, so parsing the file into memory first and then write it back to the file is something I'd rather avoid.

Any suggestions? It really doesn't have to be GSON, just something that works well.

EDIT: I have made a solution to the problem with some of the suggestions from maaartinus. You can see it here, and contribute if you know a better solution!

1 Answer 1

1

It looks like you need the array represented in the file -- otherwise I'd propose to create multiple independent records by simply appending. Here, I recommend using RandomAccessFile, replacing the closing bracket by a comma, appending the new record and the closing bracket.

Assuming you use some sane encoding1, the bracket map to a single byte (93), so finding it via a simple loop starting at the end of the file should be rather trivial. If nobody else writes to the file, you can even assume it's the last byte...

1 Do you? No, you use a platform-dependent encoding because you didn't specify the encoding the FileWriter constructor. IMHO using a platform-dependent encoding is never sane and all corresponding methods should be deprecated, but that's just my opinion.


In case individual objects (rather than an array of them) suffices, I'd recommend to write the length of each object (e.g., using DataOutputStream) followed by the entry, so you can quickly and simply jump over it. Or even better, write a separate index file containing the lengths of the records (e.g. in binary form or one numer-string per line).

Sign up to request clarification or add additional context in comments.

2 Comments

I don't really need an array. So I can just append objects all the way?
I ended up using the ´RandomAccessFile´, and I wrote a test in Java. I have no idea on how good this solution is with data usage, but at least it works. Added my solution in a link on my edit for the original question. Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.