Path.createFile() and Path.createDirectory() throw NoSuchFileException if parent doesn't exist (Windows)
Vince Bonfanti
vbonfanti at gmail.com
Tue Aug 25 12:18:01 PDT 2009
Yes, that helps. Thanks.
It may be a minor point, but I suggest that the Javadocs for Path
explicitly state that an exception is thrown if the parent doesn't
exist; maybe even list NoSuchFileException as an "optional specific
exception" as is done for FileAlreadyExistsException. Because the
Javadocs for Path are silent on this point, I wasn't sure about the
behavior until I actually tested it.
Vince
On Tue, Aug 25, 2009 at 2:59 PM, Alan Bateman<Alan.Bateman at sun.com> wrote:
> Vince Bonfanti wrote:
>>
>> I notice that on Windows both Path.createFile() and
>> Path.createDirectory() throw a NoSuchFileException if their parent
>> directory doesn't exist. Is this expected behavior for all file
>> systems, or is it optional for a file system to create the parent
>> directories if they don't exist? In either case, it should documented
>> in the Javadocs.
>>
>> Vince
>>
>
> All of Path's createXXX method are specified to throw IOException if they
> fail (a non-existent parent directory being one of many possible reasons).
> Implementations can throw more specific exceptions where they can detect
> specific errors and map them to a specific IOException. So if the parent
> directory doesn't exist then the createXXX methods should fail but it's up
> to the implementation if it throws more specific/useful IOException or not.
> In this case, we attempt to map this case to NoSuchFileException. Does that
> help? Where you need to create a file tree then the Files.createDirectories
> utility method can be used.
>
> -Alan.
>
More information about the nio-discuss
mailing list