Skip to content

saveJSONObject doesn't close the file #3705

Description

@PhiLhoSoft

Related thread in the forum: http://forum.processing.org/two/discussion/12218/why-can-t-i-read-a-json-file-with-a-different-program-while-my-sketch-is-still-open

Problematic code:

public boolean save(File file, String options) {
  return write(PApplet.createWriter(file), options);
}

write() cannot close the writer itself (it owns to the caller) but in save(), it should manage the lifetime of the writer.
So I propose something like:

public boolean save(File file, String options) {
  PrintWriter writer = PApplet.createWriter(file);
  boolean r = write(writer, options);
  writer.close();
  return r;
}

Or, cleaner, wrap the call in a try ./ finally, as write() can throw an exception.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions