3

I am creating an instance of Gson and writing with the following:

Gson gson = new GsonBuilder().setPrettyPrinting().create();
Writer writer = new FileWriter(location);
gson.toJson(data, writer);

This does write to the file, but the JSON object is incomplete and ends at line 7197 like this:

...
{
    "type": "RockCell"
},
{
    "type": "Flo

I have tried deleting and rerunning the code and it always stops writing at line 7197 in this exact way.

If I change the value of data, it ends at a different point (line 7182) but again does so consistently.

In both cases, the files are 106,496 bytes in size. This issue still occurs if pretty printing is disabled, but the files are always 49,152 bytes.

Freddy Heppell
  • 276
  • 3
  • 17
  • 4
    Do you close your FileWriter when you're done writing out to file? And if not, then why not? – Hovercraft Full Of Eels Jan 19 '18 at 22:26
  • @HovercraftFullOfEels Thank you, I did forget to add that. It's working correctly now. – Freddy Heppell Jan 19 '18 at 22:33
  • You're welcome. Closing as a duplicate to thousands of other similar questions. Please search first. A simple search on incomplete output from filewriter or something similar would have given you a solution. – Hovercraft Full Of Eels Jan 19 '18 at 22:35
  • 1
    Ill just say I could not figure this out until I stumbled upon this thread. Not sure if its a duplicate since the problem looks like its an issue with memory size at first with JSON. – NoThisIsPatrick Apr 29 '20 at 15:26

0 Answers0