title: How to unmap a file from memory mapped using FileChannel in java?
body: <p>I am mapping a file("sample.txt") to memory using <code>FileChannel.map()</code> and then closing the channel using <code>fc.close()</code>. After this when I write to the file using FileOutputStream, I am getting the following error:</p>
  
  <blockquote>
    <p>java.io.FileNotFoundException:
    sample.txt (The requested operation
    cannot be per formed on a file with a
    user-mapped section open)</p>
  </blockquote>
  
  <pre><code>File f = new File("sample.txt");
  RandomAccessFile raf = new RandomAccessFile(f,"rw");
  FileChannel fc = raf.getChannel();
  MappedByteBuffer mbf = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
  fc.close();
  raf.close();
  
  FileOutputStream fos = new FileOutputStream(f);
  fos.write(str.getBytes());
  fos.close();
  </code></pre>
  
  <p>I presume this may be due to file being still mapped to the memory even after I close the <code>FileChannel</code>. Am I right?. If so, how can I "unmap" the file from memory?(I can't find any methods for this in the API).
  Thanks.</p>
  
  <p>Edit:
  Looks like it(adding an unmap method) was submitted as RFE to sun some time back:
  <a href="http://bugs.sun.com/view_bug.do?bug_id=4724038" rel="noreferrer">http://bugs.sun.com/view_bug.do?bug_id=4724038</a></p>