views:

157

answers:

4

Hello!

I'm a bit confused with all these new File I/O classes in JDK7.

Let's say, I have a Path and want to rename the file, it represents.
How do I specify the new name, when again a Path is expected?

Path p = /* path to /home/me/file123 */;
Path name = p.getName(); /* gives me file123 */
name.moveTo(/* what now? */); /* how to rename file123 to file456? */

NOTE: Why do I need JDK7? Handling of symbolic links!

Problem is: I have to do it with files, which names and locations are known at runtime. So, what I need, is a safe method (without exceptional side-effects) to create a new name-Path of some old name-Path.

Path newName(Path oldName, String newNameString){
    /* magic */ 
}
A: 

If the destination path is identical to the source path except for the name of the file, it will be renamed rather than moved.

So for your example, the moveto path should be

/home/me/file456
pavium
So I just can create a new `Path`? Is there something simpler than: Path newName = Paths.get(name.getParent().toString()+pathSeparator+"newName"); ?
java.is.for.desktop
Yes, now the question is: how to create such path in a safe way, so that no exceptional circumstances would cause it to fail.
java.is.for.desktop
I could attempt to describe how you could take the original path, strip off the name 'file123' concatenate 'file456' instead and invoke `name.moveTo()` but since I don't really know Java, I'd be guessing. What I'm **sure** about is that a *move* becomes a *rename* if only the filename is different.
pavium
Aah, it looks like the cavalry has arrived...
pavium
A: 

If you take a look at Apache Commons IO there's a class called FileNameUtils. This does a ton of stuff wrt. file path names and will (amongst other things) reliably split up path names etc. I think that should get you a long way towards what you want.

Brian Agnew
Sorry, I need symbolic-links-aware APIs. All these Apache Commons stuff is at best Java-5-compatible, but mostly Java-1.4.
java.is.for.desktop
Does it not help you purely in terms of splitting up and reassembling filenames, and you can use Java 7 symlink aware stuff to actually perform the copy/rename ?
Brian Agnew
Good functionality for handling of symlinks is provided only by JDK7, because it has low-level (binary level) bindings to the operating system.
java.is.for.desktop
I'm afraid, that such splitting up and reassembling could in some circumstances lead to errors. But wait, I'm now experimenting with `Path.resolve(String name)`. I think that's it.
java.is.for.desktop
+1  A: 

OK, after trying everything out, it seems I found the right method:

// my helper method
Path newName(Path oldFile, String newNameString){
    // the magic is done by Path.resolve(...)
    return oldFile.getParent().resolve(newNameString);
}

// so, renaming is done by:
oldPath.moveTo(newName(oldFile, "newName"));
java.is.for.desktop
+2  A: 

You have a path string and you need to create a Path instance. You can do this with the getPath method or resolve. Here's one way:

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

Note that it checks if parent is null (looks like your solution don't do that).

Alan
People who develop java ( ;) ) should have thought of such an use-case ;)
java.is.for.desktop