RFO: open RandomAccessFile with FILE_SHARE_DELETE on Windows
Alan Bateman
alan.bateman at oracle.com
Thu Dec 25 07:22:08 UTC 2025
On 25/12/2025 07:04, Maxim Kartashev wrote:
> Hi All,
>
> Currently, the following snippet will not succeed on Windows
> try (RandomAccessFile ignored = new RandomAccessFile(f, "r")) {
> f.delete();
> }
> because RandomAccessFile is opened without the FILE_SHARE_DELETE
> option for the CreateFileW() call.
> I wonder if this is intentional and what, if anything, would break if
> this decision is reversed and FILE_SHARE_DELETE is supplied? I suspect
> applications rely on the existing semantics but can't give concrete
> examples myself.
>
Files opened with APIs introduced since JDK 7 use FILE_SHARE_DELETE by
default.
The issue with the legacy APIs dates from early JDK releases and has
been looked at many times over the years. Many of these cases where it
comes up involved JarFile where tools are deleting JAR files that are in
use. Some time ago we had a prototype re-write of java.io on top of
java.nio.file and has to specifically deal with the issue.
So yes, long standing issues that dates from early JDK releases.
Changing the behavior would be a significant change and we have never
been brave enough to do it. Also FILE_SHARE_DELETE is not the full
story, there are a bunch of scenarios where Windows doesn't provide the
same semantics as Unix, e.g. re-creating files, deleting files that have
file mappings, sub-processes, ...
-Alan
More information about the nio-dev
mailing list