title: Can't delete a file after editing with JSONObject and ObjectMapper
body: <p>I am using java to edit a file that contains JSON:</p>
  
  <pre><code>public void editApi(String path, String nameOfOldApi, API updatedApi)
      throws IOException, ParseException
  {
      String apiPath = path + "\\" + updatedApi.getTeamName() + "\\";
      Gson gson = new Gson();
      ObjectMapper mapper = new ObjectMapper();
  
      String updatedJsonString = gson.toJson(updatedApi);
      JSONObject jsonToBeEditted = mapper.readValue(new File(apiPath + nameOfOldApi + ".json"), JSONObject.class);
      JSONObject updatedJson = mapper.readValue(updatedJsonString, JSONObject.class);
  
      for (Object key : jsonToBeEditted.keySet()) {
          if (key.equals("tags") || key.equals("paths")) {
              continue;
          } else if (!jsonToBeEditted.get(key).equals(updatedJson.get(key))) {
              System.out.println(jsonToBeEditted.get(key) + "---&gt;" + updatedJson.get(key));
              jsonToBeEditted.put(key, updatedJson.get(key));
          }
      }
  
      mapper.writeValue(new File(apiPath + updatedApi.getInfo().getTitle() + ".json"), jsonToBeEditted);
  
      if (!updatedApi.getInfo().getTitle().equals(nameOfOldApi)) {
          Files.delete(new File(apiPath + nameOfOldApi + ".json").toPath());
      }
  }
  </code></pre>
  
  <p>When I run the program (which is a spring application, if that's relevant) and make a call to this, it works fine the first time. But if I try and edit that same file again, I get a FileSystemException saying the file is being used by another process. eg, if I have a file 'test.json' and I edit it to 'test edit.json', it correctly adds the new file and deletes the old file. However, if I then edit 'test edit.json' to 'test edit again.json', it throws this error:</p>
  
  <pre class="lang-none prettyprint-override"><code>java.nio.file.FileSystemException: C:\stackoverflow.json: The process cannot access
  the file because it is being used by another process.
  </code></pre>
  
  <p>And I'm lost as to where it happens... I feel like something isn't being closed properly in the <code>ObjectMapper.writeValue()</code> call, but I'm not sure.</p>