Existence of a file fails if it is created with SBC(C_N, W, D_ON_C) (Sol)
Alan Bateman
Alan.Bateman at Sun.COM
Thu Sep 25 03:08:46 PDT 2008
Rajendra Gutupalli wrote:
> :
>
> I have one more query. Is it okay to create a file to which
> SekableByteChannel is opened for DELETE_ON_CLOSE. ie. In the following
> code snippet Should path.createFile() succeed or should it throw
> FileAlreadyExistsException?
>
It cannot be specified and is essentially the same issue in a different
form. You will see exactly the same if do you the following:
Path file = ...
OutputStream out = file.newOutputStream();
file.delete();
file.createFile();
That is, on Windows, the second attempt to create the file will throw an
exception because the file remains "visible" in the file system until
all open handles are closed. The reason you don't see this with the
java.io package today is because FIS/FOS/RAF open files without the
delete sharing option and this prevents open files from being deleted (a
major gripe for many developers). In the new API then the delete option
is enabled by default but if developers really want to do Windows
specific things then there are extended open options beyond the spec
that can be used to set the DOS sharing mode when opening a file.
In any case, one of the intended uses of the DELETE_ON_CLOSE option is
with files in the temporary directory so you can do this:
SeekableByteChannel sbc = File.createTempFile("blah", "tmp")
.newSeekableByteChannel(READ, WRITE, DELETE_ON_CLOSE);
which gives you a channel to a temporary file that will be reclaimed
when you finished with it or the VM terminates. In that scenario the
file name is generated to be unique.
-Alan.
More information about the nio-dev
mailing list