Java IO - Rename / Move File

May 16, 2020 No comments Java IO Rename Move File

1. Introduction

In this article, we are going to present several ways to rename or move File in Java using JDK 6,7 and libraries such as Guava and Apache Commons IO.

This article is a part of the Java IO Tutorial.

2. Rename the file using JDK 6

Plain Java comes with methods that covered common operations on Files. In JDK 6 we can use renameTo(...) method available under java.io.File object:

package com.frontbackend.java.io.move;

import java.io.File;

public class RenameFileUsingFileObject {

    public static void main(String[] args) {
        File file = new File("/tmp/fileToRename.txt");

        boolean renamed = file.renameTo(new File("/tmp/out.txt"));
        if (renamed) {
            System.out.println("File successfully renamed");
        } else {
            System.out.println("File cannot be renamed");
        }
    }
}

Note that this old method will not throw an exception if the source file does not exist. If we need the information that operation finished successfully we must check the result value.

3. Renaming the file using Files class from JDK 7

In modern JDK 7 we can use Files object that operates on Paths, check the following example:

package com.frontbackend.java.io.move;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class RenameFileUsingFilesObject {

    public static void main(String[] args) throws IOException {
        File file = new File("/tmp/fileToRename.txt");
        File dest = new File("/tmp/out.txt");

        Files.move(file.toPath(), dest.toPath());
    }
}

In this snippet, we made use of Files.move(...) method that takes two parameters:

  • the path to the file to move,
  • the path to the target file.

Note that if the file does not exist an exception will be thrown:

Exception in thread "main" java.nio.file.NoSuchFileException: /tmp/fileToRename.txt

4. Renaming file using Guava library

The Guava library also allows us to rename/move a file:

package com.frontbackend.java.io.move;

import java.io.File;
import java.io.IOException;

import com.google.common.io.Files;

public class RenameFileUsingGuava {

    public static void main(String[] args) throws IOException {
        File file = new File("/tmp/fileToRename.txt");
        File dest = new File("/tmp/out.txt");

        Files.move(file, dest);
    }
}

Guava using similar to the JDK 7 approach in moving/renaming files. If the source file or destination directory does not exist we will get an exception like the following:

Exception in thread "main" java.io.FileNotFoundException: /tmp/fileToRename.txt (No such file or directory)

5. Rename the file with Apache Commons IO library

The Apache Commons IO library is another way to rename/move a file in Java:

package com.frontbackend.java.io.move;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class RenameFileUsingGuava {

    public static void main(String[] args) throws IOException {
        File file = new File("/tmp/fileToRename.txt");
        File dest = new File("/tmp/out.txt");

        FileUtils.moveFile(file, dest);
    }
}

In this example, we used FileUtils.modeFile(...) method.

6. Conclusion

In this article, we presented several ways to rename/move a file in Java using JDK6, JDK7, Guava, and Apache Commons IO library.

Code snippets used in this tutorial are available under the GitHub repository.

{{ message }}

{{ 'Comments are closed.' | trans }}