How to Move File From Current Directory to a New Directory in Java

Mehvish Ashiq Feb 02, 2024
  1. Use the Files.move() Method of Java NIO to Move File From the Current Directory to a New Directory
  2. Use the renameTo() Method to Move a File From the Current Directory to a New Directory
How to Move File From Current Directory to a New Directory in Java

This tutorial presents different ways to move a file in Java. We will learn about the two methods to move a file from one directory to another directory locally (on the same machine).

These methods include the Files.move() method of Java NIO (New Input Output) package and the renameTo() method that is contained by Java.io.File package.

Use the Files.move() Method of Java NIO to Move File From the Current Directory to a New Directory

We can use the following solution if we have Java 7 or above.

Example code:

import java.io.IOException;
import java.nio.file.*;

public class MoveFile {
  public static void main(String[] args) {
    Path sourcePath = Paths.get("./moveFile.txt");
    Path targetPath = Paths.get(System.getProperty("user.home") + "/Desktop/Files/moveFile.txt");

    try {
      Files.move(sourcePath, targetPath);
    } catch (FileAlreadyExistsException ex) {
      System.out.println("The target path already has a target file");
    } catch (IOException exception) {
      System.out.format("I/O error: %s%n", exception);
    }
  }
}

The main method requires the source path and the target path for a file that needs to be moved. We use the Paths.get() method to retrieve the path of the source file and save it into a Path type variable named sourcePath.

Then, we use the Paths.get() method again to get the target path by concatenating the desired location and the user’s home directory, which is accessed by using the System.getProperty("user.home").

The Files.move() method takes the source path and the target path for a file that needs to be moved and moves it from the sourcePath to the targetPath without changing the file’s name.

The Files.move() method is enclosed within the try block because it may cause the IOException or FileAlreadyExistsException. However, these exceptions are properly handled in the catch blocks.

If we want to avoid the FileAlreadyExistException, then we can replace the file at targetPath by using the REPLACE_EXISTING option as follows:

Files.move(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);

And if we want to have a file with a new name at targetPath, then we will keep the sourcePath and targetPath same but change the file’s name only as follows:

Path sourcePath = Paths.get("./moveFile.txt");
Path targetPath = Paths.get(System.getProperty("user.home") + "/Desktop/Files/newMoveFile.txt");

Use the renameTo() Method to Move a File From the Current Directory to a New Directory

The following solution is preferred if we have Java 6.

Example code:

import java.io.*;

public class MoveFile {
  public static void main(String[] args) {
    File file = new File(".\\moveFile.txt");

    if (file.renameTo(new File("E:\\moveFile.txt"))) {
      System.out.println("File is moved successfully");
    } else {
      System.out.println("File is not moved");
    }
  }
}

This code snippet also moves the specified file from one location to another using the renameTo() method.

The renameTo() method takes a new abstract destination path, renames the file (if we specify a new name but do not rename it for this solution), and moves the file to the target location.

It returns true if the file is moved successfully, and we are printing a message to show that the file is moved and display a warning if the given file is not moved. You may see the code given above.

Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook

Related Article - Java File