我正在使用文件("sample.txt")映射到内存FileChannel.map(),然后使用关闭通道fc.close().在此之后,当我使用FileOutputStream写入文件时,我收到以下错误:
java.io.FileNotFoundException:sample.txt(无法在打开用户映射部分的文件上形成请求的操作)
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();
Run Code Online (Sandbox Code Playgroud)
我认为这可能是由于文件仍然映射到内存,即使我关闭后FileChannel.我对吗?.如果是这样,我如何从内存中"取消映射"文件?(我在API中找不到任何方法).谢谢.
编辑:看起来像(添加一个unmap方法)作为RFE提交给sun一段时间了:http : //bugs.sun.com/view_bug.do?bug_id=4724038