Rename a File in Java

Java provides two common ways to rename a file: the older File.renameTo() method and the newer NIO Files.move() method. Both can rename a file, but Files.move() gives clearer error handling and more control over what happens when the destination already exists.

If you are maintaining older code, File.renameTo() may be sufficient for a simple rename. For new code, Files.move() is usually the better choice because it reports failures with exceptions instead of only returning false.

Rename a File using File.renameTo()

The renameTo() method is called on the File object that represents the current file. Pass another File object containing the destination path and new file name.

  1. Create a File object for the file you want to rename.
  2. Create another File object for the destination path and new file name.
  3. Call renameTo() on the original file and pass the destination File object.
  4. Check the returned boolean value to determine whether the rename succeeded.

The method returns true only when the rename succeeds. It returns false when the operation fails. Its behavior is platform-dependent, so the return value should always be checked.

Example: rename data.txt to newdata.txt with File.renameTo()

In this example, both paths are inside the same files directory, so the operation changes the file name from data.txt to newdata.txt.

RenameFile.java

</>
Copy
import java.io.File;

/**
 * Java Example Program to Rename File
 */

public class RenameFile {

	public static void main(String[] args) {
		File originalFile = new File("files/data.txt");
		File newFile = new File("files/newdata.txt");
		
		//rename file
		boolean isRenameDone = originalFile.renameTo(newFile);
		
		//print if the rename is successful
		System.out.println("Rename Done: "+isRenameDone);
	}
}

Output

Rename Done: true

The output is true, which means the rename operation succeeded for this run.

File.renameTo() when the source file does not exist

If the source path does not identify an existing file that can be renamed, the operation fails and renameTo() returns false.

RenameFile.java

</>
Copy
import java.io.File;

/**
 * Java Example Program to Rename File
 */

public class RenameFile {

	public static void main(String[] args) {
		File originalFile = new File("files/data1.txt");
		File newFile = new File("files/newdata.txt");
		
		//rename file
		boolean isRenameDone = originalFile.renameTo(newFile);
		
		//print if the rename is successful
		System.out.println("Rename Done: "+isRenameDone);
	}
}

Here, files/data1.txt is not present, so the rename cannot be completed.

Output

Rename Done: false

File.renameTo() when the destination file already exists

Another common failure case occurs when the destination name is already in use. The exact behavior of renameTo() is platform-dependent, and it may fail when the destination file already exists.

RenameFile.java

</>
Copy
import java.io.File;

/**
 * Java Example Program to Rename File
 */

public class RenameFile {

	public static void main(String[] args) {
		File originalFile = new File("files/data1.txt");
		File newFile = new File("files/data.txt");
		
		//rename file
		boolean isRenameDone = originalFile.renameTo(newFile);
		
		//print if the rename is successful
		System.out.println("Rename Done: "+isRenameDone);
	}
}

For the run shown below, the method returns false. Because renameTo() does not provide a detailed exception explaining the failure, it is often harder to diagnose than the NIO approach.

Output

Rename Done: false

Rename a File with java.nio.file.Files.move()

The java.nio.file.Files.move() method can move or rename a file. To rename a file, use a target path in the same directory with a different file name.

</>
Copy
Files.move(sourcePath, targetPath);

Unlike File.renameTo(), Files.move() throws an IOException when the operation cannot be completed. This makes it possible to handle missing files, existing destination files, permission problems, and other I/O errors more explicitly.

Example: rename data.txt with Files.move()

</>
Copy
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class RenameFileNio {
    public static void main(String[] args) {
        Path source = Path.of("files/data.txt");
        Path target = Path.of("files/newdata.txt");

        try {
            Files.move(source, target);
            System.out.println("File renamed successfully.");
        } catch (IOException e) {
            System.out.println("Could not rename file: " + e.getMessage());
        }
    }
}

If files/data.txt exists and files/newdata.txt can be created, the file is renamed. By default, the operation fails if the destination already exists.

Rename and Replace an Existing File with Files.move()

If the destination file should be replaced, pass StandardCopyOption.REPLACE_EXISTING to Files.move().

</>
Copy
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class RenameAndReplaceFile {
    public static void main(String[] args) throws IOException {
        Path source = Path.of("files/data.txt");
        Path target = Path.of("files/newdata.txt");

        Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);
    }
}

Use REPLACE_EXISTING only when overwriting the destination is intentional. Without this option, Files.move() does not replace an existing target file by default.

Move a File to Another Directory and Rename It

The same Files.move() call can change both the directory and the file name. Make the target path point to the new directory and use the desired new name.

</>
Copy
Path source = Path.of("files/data.txt");
Path target = Path.of("archive/renamed-data.txt");

Files.move(source, target);

This moves data.txt from the files directory into archive and renames it to renamed-data.txt. The target directory must be available for the move to succeed.

Why a Java File Rename Can Fail

  • The source file does not exist: verify the source path before attempting the rename.
  • The destination already exists: use a different name or, with Files.move(), explicitly use REPLACE_EXISTING when replacement is intended.
  • The application lacks permission: the process must have the required access to the source and destination locations.
  • The source and destination are on different file systems: a simple rename may not be supported as the same kind of operation across file systems.
  • An atomic move is requested but unsupported: StandardCopyOption.ATOMIC_MOVE can fail with AtomicMoveNotSupportedException when the file system cannot perform the move atomically.

File.renameTo() vs Files.move() for Renaming Files

AspectFile.renameTo()Files.move()
APIjava.io.Filejava.nio.file.Files
Failure reportingReturns falseThrows an IOException
Replace existing targetBehavior can depend on the platformUse REPLACE_EXISTING
Atomic move optionNo explicit optionSupports ATOMIC_MOVE where available
Best fitSimple or legacy codeNew code that needs clearer control and error handling

For new Java code, prefer Files.move() when you need predictable options and useful exceptions. Use File.renameTo() when working with an existing File-based API and remember to check its boolean return value.

Java File Rename Summary

To rename a file in Java, you can call File.renameTo() and check whether it returns true, or use Files.move() with a source and target Path. The NIO method is generally easier to diagnose when something goes wrong and also supports options such as replacing an existing destination or requesting an atomic move.

In this Java Tutorial, we learned how to rename a File in Java and how to be sure if the rename operation is successful.