Questions on the NIO.2 API

Martijn Verburg martijnverburg at gmail.com
Mon Oct 11 09:36:44 PDT 2010


Hi all,

A bit of background first :).  Along with my co-author (Ben Evans) I'm
at the early stages of writing a book covering modern Java developer
topics.  This will include the NIO.2 API changes for Java 7 (which we
think are an important step forward for Java).  I contacted Alan a
whole back and he suggested I submit any issues/thoughts we had with
the API here.  In particular we're focussing on what a fairly new
developer to Java might find awkward or difficult (we're testing our
samples with lots of grads/undergrads).

Apologies if some of this has been discussed before - I try to scan
the archives for answers in most cases, but sometimes my research
skills fail me :(.  The first example we take our readers through is
the relationships between FileSystem, Path, FileStore and pals.  The
listing below simply takes the user through a fairly patsy example of
copying a file over (inefficiently I might add, but I'm trying to
explore some of the core classes for now).  Feel free to skip the code
and jump to the bottom, the code is there just for completeness.

private void listing2_1() {

            FileSystem fileSystem = FileSystems.getDefault();
            Path timeSheetFile =
fileSystem.getPath("C:/projects/timesheet.txt");
            Path backupDir = fileSystem.getPath("H:/projects/");
            try
            {
                  timeSheetFile.checkAccess(AccessMode.READ);
                  backupDir.checkAccess(AccessMode.WRITE);
                  Path backupFile =
fileSystem.getPath("H:/projects/timesheet.txt");
                  FileStore backupFileStore = backupDir.getFileStore();
                  if (!backupFileStore.type().equals("NTFS")) {
                        throw new IOException("Suspicious store type");
                  }
                  CopyOption copyOptions = StandardCopyOption.REPLACE_EXISTING;
                  timeSheetFile.copyTo(backupFile, copyOptions);
                  System.out.println(backupFile.toAbsolutePath());
            }
            catch (IOException e)
            {
                  e.printStackTrace();
            }
}

So some things I thought were really cool when using this API for the
first time:

* FileSystems.getDefault() - Great time saving shortcut, easy for new developers
* Being able to check access modes directly on a Path
* The concept of a FileStore - fantastic
* CopyOptions etc - I'll comment on in another example, but I liked
this, again easy for developers.

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).

* 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).

----------

Is this sort of feedback useful to this list?  Or is it just going to
waste your valuable time?  I've worked on a number of open source
projects, so I know what it can be like :).

Disclaimer:  I'm not a low-level API designer by any stretch of the
imagination, so if it's the case that you only want bug reports as
opposed to ill-thought out suggestions then I understand!

Cheers,
Martijn (@java7developer - twitter)


More information about the nio-dev mailing list