Deleting symbolic links via the java.nio.file.Path API

Alan Bateman Alan.Bateman at oracle.com
Mon Oct 11 10:09:05 PDT 2010


fm wrote:
> Sorry. I was being careless when I threw the checkAccess method into
> my conversation...I didn't think that one through. You are right,
> checkAccess does not seem to make sense for the symbolic links
> themselves (as opposed to their targets).
>
> However, I believe my concern with respect to checking the existence
> of symbolic links with the exists method is still reasonable.
>
> Just to provide another example. The following code throws a
> FileAlreadyExistsException when somePath corresponds to a broken
> symbolic link.
>
>    try {
>       if (!somePath.exists())       // or use somePath.notExists()
>          somePath.createFile();
>    }
>    catch (FileAlreadyExistsException e) {
>       System.err.println("File exists!?!");
>    }
>    catch (IOException e) { }
>
> Being able to call something like
> "somePath.exists(LinkOption.NOFOLLOW_LINKS)" would be helpful
>   
Yes, this could help for this case but the above may be still with 
FailAlreadyExistsException if there are other threads (or other 
processes) competing to create the file. For this specific case then the 
following might be closer to what you want:
  file.newOutputStream(CREATE).close();

-Alan


More information about the nio-dev mailing list