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

assembling signals assembling.signals at yandex.ru
Thu Dec 17 02:21:49 UTC 2009


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 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 cut-and-slice some string around, and create a destination Path of the result.
Something like:

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

What do you think about the problem?

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



More information about the core-libs-dev mailing list