title: How to delete file in Java if locked by a process?
body: <p>I decided to post a new question on this since none of the existing posts lead to me a solution. Mine is a Spring Boot application and here is the service:</p>
  <pre><code>public String fetchPrediction(MultipartFile file) throws IOException, InterruptedException {
          File convFile = new File( System.getProperty(&quot;user.dir&quot;)+&quot;/&quot;+file.getOriginalFilename());
          convFile.setWritable(true);
          file.transferTo(convFile);
          INDArray array = new CustomerLossPrediction().generateOutput(convFile);
          Files.delete(Paths.get(convFile.getPath()));
          return array.toString();
      }
  </code></pre>
  <p>File deletion isn't happening and it gets stored at user home directory:
  <a href="https://i.stack.imgur.com/XV2mM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XV2mM.png" alt="enter image description here" /></a></p>
  <p>Found that the file is being used by the Java process. How can I delete this file once execution is completed? Is there a better approach here rather than writing to a file? Some of you would bring up writing to <code>OutputStream</code> here, but note that I need to work with <code>MultipartFile</code> in order to have file upload functionality.</p>