Rename a File in Java

  1. Rename a File Using the renameTo() Method in Java
  2. Rename a File Using the move() Method in Java
  3. Rename a File Using the move() Method in Java
  4. Rename a File Using the Apache commons Library in Java

This tutorial introduces how to rename a file in Java and lists some example codes for you to understand the topic further.

Renaming a file in Java is pretty easy as Java provides several built-in methods in the java.io package. We can use these methods to rename a file and check other file operations as well. In this article, we will use the renameTo() method of the File class, the move() method of the Files class, and the Apache commons library to rename the file.

Rename a File Using the renameTo() Method in Java

In this example, we are using the File class to get the instance of the file, and then by using the renameTo() method, we renamed the file. This method returns an IOException, so you must use a proper try-catch block to handle the exception. The renameTo() method returns a boolean value, either true or false, that can be used to check whether a file is renamed successfully.

import java.io.File;
import java.io.IOException;
public class SimpleTesting{
    public static void main(String[] args) throws IOException {
        File file1 = new File("abc.txt");
        File file2 = new File("abcd.txt");
        if (file2.exists())
        throw new java.io.IOException("file exists");
        boolean success = file1.renameTo(file2);
        if (success) {
            System.out.println("File Rename successfuly");
        }else System.out.println("File is not Rename");
    }
}

Output:

File Rename successfuly

Rename a File Using the move() Method in Java

This method is another solution to rename a file. Here, we used the move() method of the Files class, which can be used to make a file renamed. See the example below:

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class SimpleTesting{
    public static void main(String[] args) {
        try {
            Path source = Paths.get("/file-location/abc.txt");
            Files.move(source, source.resolveSibling("/file-location/abcd.txt"));
        }catch(Exception e) {
            System.out.println(e);
        }
    }
}

Rename a File Using the move() Method in Java

The move() method has one overloading method that takes a file-path as a second parameter. So, if you want to move a file to another location after the rename process, you can set this parameter in the function call.

import java.io.File;
import java.nio.file.Files;
public class SimpleTesting{
    public static void main(String[] args) {
        try {
            File newFile = new File(new File("/file-location/abc.txt").getParent(), "abcd.txt");
            Files.move(new File("/file-location/abc.txt").toPath(), newFile.toPath());
        }catch(Exception e) {
            System.out.println(e);
        }
    }
}

Rename a File Using the Apache commons Library in Java

If you are working with the Apache commons Java library, you can use the moveFile() method of the FileUtils class. Check the example program here:

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class SimpleTesting{
    public static void main(String[] args) {
        File file = new File("/file-location/abc.txt");
        String newName = "abcd.txt";
        String newFilePath = file.getAbsolutePath().replace(file.getName(), "") + newName;
        File newFile = new File(newFilePath);
        try {
            FileUtils.moveFile(new File("/file-location/abc.txt"), newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Java File

  • How to Read XML File in Java
  • Read All Files of a Folder in Java