title: Unable to delete a file after reading it
body: <p>I'm serving a file from the file system <a href="https://stackoverflow.com/questions/2114488/how-to-serve-a-file-with-jsp">dynamically with a jsp</a></p>
  
  <p>Here's my code: </p>
  
  <pre><code>&lt;%@ page import="java.io.*,java.util.*"
      InputStream in = null;
      OutputStream responseOut = null;
      File  file  = new File(request.getAttribute("fileToServe"));
      try{
          in = new FileInputStream(file);
          responseOut = response.getOutputStream();
          // Transfer bytes from in to out
          byte[] buf = new byte[1024];
          int len;
          while ((len = in.read(buf)) &gt; 0) {
              responseOut.write(buf, 0, len);
          }
      }finally{
           if( responseOut != null ) try {
              responseOut.close();
           } catch( IOException ioe ){}
  
          if( in != null ) try {
              in.close();
           } catch( IOException ioe ){}
      }
      file.delete();
  %&gt;
  </code></pre>
  
  <p>The problem I'm facing is, the file is delete only the first time the code is run, which is after the server restart. Subsequent calls doesn't delete the file.</p>
  
  <p>I used ProcessExplorer to track this and and yeap, the Java VM is holding that file, I don't really know why is this happening. </p>
  
  <p>We will run on Windows OS, always, is there a work around for this?</p>
  
  <p>I've found a number of resources on the internet about this, but I can't figure out from them how to solve this problem. </p>