22

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 whose 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 */ 
}
MasterJoe
  • 2,103
  • 5
  • 32
  • 58
java.is.for.desktop
  • 10,748
  • 12
  • 69
  • 103

6 Answers6

61

In JDK7, Files.move() provides a short and concise syntax for renaming files:

Path newName(Path oldName, String newNameString) {
    return Files.move(oldName, oldName.resolveSibling(newNameString));
}

First we're getting the Path to the new file name using Path.resolveSibling() and the we use Files.move() to do the actual renaming.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Avner
  • 5,791
  • 2
  • 29
  • 33
  • This has the added bonus of being platform independent, unlike moveTo that can break occasionally. – Leon Nov 15 '18 at 13:58
9

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

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Alan
  • 121
  • 1
4

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
  • 10,748
  • 12
  • 69
  • 103
1

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
  • 268,207
  • 37
  • 334
  • 440
  • 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 Dec 16 '09 at 13:09
  • 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 Dec 16 '09 at 13:12
  • 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 Dec 16 '09 at 13:12
  • 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 Dec 16 '09 at 13:14
0

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
  • 14,808
  • 4
  • 33
  • 50
  • 1
    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 Dec 16 '09 at 12:54
  • 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 Dec 16 '09 at 12:59
  • 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 Dec 16 '09 at 13:02
-2

If you can't get Java to do what you want with Unix I recommend Python scripts (run by your Java program). Python has get support for Unix scripting and it's not Perl :) This might sound inelegant to you but really in a larger program you'll benefit from using the right tool for the job.

Glen P
  • 719
  • 5
  • 10