Questions on the NIO.2 API
Alan Bateman
Alan.Bateman at oracle.com
Mon Oct 11 11:17:40 PDT 2010
Martijn Verburg wrote:
> :
>
> Things I wondered about (which there are probably good answers for!):
>
> * There seems to be no direct way to query the FileSystem or Path
> whether you're dealing with a directory as opposed to a file (or other
> type of Path), something like isDir(). This is something a new
> developer is likely to 'expect' in an API (perhaps in a specific
> implementation of a Path or FileSystem).
>
Yes, we need to do something about this. The awkward issue has always
been that isDirectory (or any isXXX method to test the file type) can
fail for various reasons and so technically the answer isn't a boolean
but rather one of three answers (it's a directory, it's not a directory,
or it failed). For now, the workaround is to do something like this:
BasicFileAttributes attrs = Attributes.readBasicFileAttributes(file);
if (attrs.isDirectory()) {
:
}
but of course when you go to access the file then it might not be a
directory because someone/something replaced the file in the mean-time.
> * getPath(String URI) doesn't throw any Exceptions if the underlying
> file doesn't exist. Only when you try to utilise the non-existent
> Path does it throw an Exception. Now I assume this is a deliberate
> design choice (Path's shouldn't be tied to physical resources until
> needed) but I was wondering if would be sensible to add something like
> a getPath(String URL, boolean bind) throws NoSuchFileException method
> so that developers could have the check made immediately (if bind ==
> true).
>
A side comment, but the parameter to getPath is a path string (not a
URI, at least not in the case of the default provider).
So is checking if the file exists when you create the Path really
useful? There's no guarantee that the file will still exist when you do
to access it later. If you really want to verify that you've got a path
to a real file then you can test it with the exists method, or if you
want an exception, use checkAccess.
>
> Is this sort of feedback useful to this list?
>
Yes, definitely. I'm particularly interested, as I'm sure are others, to
hear how you get on with the samples that you are giving the students.
These students will be the developers that will hopefully be using the
platform when they graduate and move into jobs.
-Alan.
More information about the nio-dev
mailing list