NIO2's Path.moveTo(...) needs renameTo(...) aequivalent,or at least javadoc clarification.
Salter, Thomas A
Thomas.Salter at unisys.com
Thu Dec 17 11:50:20 PST 2009
It seems like Path could use a getSibling(String name) method which effectively replaces the last node of the path with the name parameter. Then your code could be:
oldFile.moveTo( oldFile.getSibling( newNameString ) );
I'd guess it's a fairly common occurrence to want to get the name of a file in the same directory as a known file, like a .h that goes with a .c file.
Likewise, a variant of subpath that returns just the last node would be useful, rather than writing:
int lastNode = file.getNameCount() - 1;
Path last = file.subpath(lastNode, lastNode);
From: nio-dev-bounces at openjdk.java.net [mailto:nio-dev-bounces at openjdk.java.net] On Behalf Of assembling signals
Sent: Thursday, December 17, 2009 2:06 PM
To: nio-dev at openjdk.java.net
Subject: Fwd: NIO2's Path.moveTo(...) needs renameTo(...) aequivalent,or at least javadoc clarification.
As suggested by Alan Bateman, I'm sending this mail to the nio-dev mailinglist:
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);
Got the problem and the code from here:
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.
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