How to Move a File in Java

1. Overview does not contains any ready make move file method, but you can us renameTo() method to move a file.

2. Move File Example

  1. Create a file named "sample.txt" in directory "C:/workspace".
  2. Create File class object by passing file absolute location path "C:/workspace/sample.txt".
  3. We need to passs new abstract pathname to renameTo() method to move the file.
  4. renameTo() method returns true if and only if the renaming succeeded; false otherwise.
  5. Observe the directory whether the file is moved or not.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

 * This Java program demonstrates how to move file in Java.
 * @author

public class MoveFileExample {

 private static final Logger LOGGER = LoggerFactory.getLogger(MoveFileExample.class);

 public static void main(String[] args) {

 public static void moveFile() {
  File file = new File("C:/workspace/sample.txt");
  boolean move = file.renameTo(new File("C:/workspace/moved/sample.txt"));
  if (move) {"File is moved successful!");
  } else {"File is failed to move!");

3. Reference