Fwd: NIO2's Path.moveTo(...) needs renameTo(...) aequivalent, or at least javadoc clarification.

assembling signals assembling.signals at yandex.ru
Thu Dec 17 11:06:22 PST 2009


As suggested by Alan Bateman, I'm sending this mail to the nio-dev mailinglist:

Hello, everyone!

It seems to be complicated/confusing to rename (not move) a file (a Path) using NIO2's Path.moveTo(...).

Following code is necessary:

    Path dir = oldFile.getParent();
    Path fn = oldFile.getFileSystem().getPath(newNameString);
    Path target = (dir == null) ? fn : dir.resolve(fn);
    oldFile.moveTo(target);

Got the problem and the code from here:
http://stackoverflow.com/questions/1914474/how-do-i-rename-not-move-a-file-in-jdk7

Wouldn't it be good, to create a Path.renameTo(String newName) method?
Or at least add clarification in javadoc? (Which is far less appropriate, an API has to "talk" to the user by classes and methods names)

I'm afraid, most people will start using File.renameTo(File newFile), which would fail to handle symbolic-link correctly.
Or, what is even worse, people will try to concat some strings, and create a destination Path of the result.

Something like:
    Path newName = Paths.get(name.getParent().toString()+File.pathSeparator+"newName");

While someone would even do:
    name.getParent().toString()+"\\"+"newName", or name.getParent().toString()+"/"+"newName"

What do you think about the problem?

Best regards, Ivan G Shevchenko
-- assembling dot signals at yandex dot ru


More information about the nio-dev mailing list