How do I rename (not move) a file in Java 7?

24,073

Solution 1

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

Solution 2

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.

Solution 3

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"));

Solution 4

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.

Share:
24,073

Related videos on Youtube

java.is.for.desktop
Author by

java.is.for.desktop

Updated on July 09, 2022

Comments

  • java.is.for.desktop
    java.is.for.desktop almost 2 years

    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 */ 
    }
    
  • java.is.for.desktop
    java.is.for.desktop over 14 years
    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
    java.is.for.desktop over 14 years
    Yes, now the question is: how to create such path in a safe way, so that no exceptional circumstances would cause it to fail.
  • pavium
    pavium over 14 years
    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.
  • java.is.for.desktop
    java.is.for.desktop over 14 years
    Sorry, I need symbolic-links-aware APIs. All these Apache Commons stuff is at best Java-5-compatible, but mostly Java-1.4.
  • Brian Agnew
    Brian Agnew over 14 years
    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 ?
  • java.is.for.desktop
    java.is.for.desktop over 14 years
    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
    java.is.for.desktop over 14 years
    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.
  • Leon
    Leon over 5 years
    This has the added bonus of being platform independent, unlike moveTo that can break occasionally.