Moving a file from one directory to another using Java Last Updated : 21 Oct, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Java provides functions to move files between directories. Two ways to achieve this are described here. The first method utilizes Files package for moving while the other method first copies the file to destination and then deletes the original copy from the source. Using Files.Path move() method: Renaming and moving the file permanently to a new location. Syntax: public static Path move(Path source, Path target, CopyOption..options) throws IOException Parameters: source - the path to the file to move target - the path to the target file (may be associated with a different provider to the source path) options - options specifying how the move should be done Returns: the path to the target file Java // Java program to illustrate renaming and // moving a file permanently to a new location import java.io.*; import java.nio.file.Files; import java.nio.file.*; public class Test { public static void main(String[] args) throws IOException { Path temp = Files.move (Paths.get("C:\\Users\\Mayank\\Desktop\\44.txt"), Paths.get("C:\\Users\\Mayank\\Desktop\\dest\\445.txt")); if(temp != null) { System.out.println("File renamed and moved successfully"); } else { System.out.println("Failed to move the file"); } } } Output: File renamed and moved successfullyUsing Java.io.File.renameTo() and Java.io.File.delete() methods: Copying the file and deleting the original file using these two methods. Syntax of renameTo(): public boolean renameTo(File dest) Description: Renames the file denoted by this abstract path name. Parameters: dest - The new abstract path name for the named file Returns: true if and only if the renaming succeeded; false otherwise Syntax of delete(): public boolean delete() Description: Deletes the file or directory denoted by this abstract path name. Returns: true if and only if the file or directory is successfully deleted; false otherwise Java // Java program to illustrate Copying the file // and deleting the original file import java.io.*; public class Test { public static void main(String[] args) { File file = new File("C:\\Users\\Mayank\\Desktop\\1.txt"); // renaming the file and moving it to a new location if(file.renameTo (new File("C:\\Users\\Mayank\\Desktop\\dest\\newFile.txt"))) { // if file copied successfully then delete the original file file.delete(); System.out.println("File moved successfully"); } else { System.out.println("Failed to move the file"); } } } Output File moved successfully References: Oracle Comment More infoAdvertise with us Next Article How to run java class file which is in different directory? K kartik Improve Article Tags : Java Java-Library Practice Tags : Java Similar Reads How to find and open the Hidden files in a Directory using Java Pre-requisites: Java File Handling So far the operations using Java programs are done on a prompt/terminal which is not stored anywhere. But in the software industry, most of the programs are written to store the information fetched from the program. One such way is to store the fetched information 3 min read How to rename all files of a folder using Java? When transferring files from the camera folder to a workspace where we would like to analyze the pictures, it becomes difficult to deal with long files and type them out again and again when testing them through code. Also, the number of files might be too large to manually rename each one of them. 4 min read How to Check a File or Directory Exists in Java? One of the most frequent tasks carried out by a file system in an operating system is checking the existence of a directory or a file. In the form of library functions, most programming languages provide some level of file system accessibility. You will discover how to test an existing file or direc 1 min read How to run java class file which is in different directory? In this article, we will learn about how to use other project's utilities, classes, and members. Before proceeding let's learn about some keywords. classpath Classpath is the location from where JVM starts execution of a program. Similar to the classic dynamic loading behavior, when executing Java p 6 min read Files createTempDirectory() Method in Java with Examples The createTempDirectory() method of java.nio.file.Files Class is used to create a new directory using the given prefix and attributes. The given prefix acts as the name of the formed directory, may be null. The directory is set with given attributes. Based on the type of arguments passed, the Files 4 min read Delete a File Using Java Java provides methods to delete files programmatically. In contrast to normal delete operations in any operating system, files being deleted using the Java program are deleted permanently without being moved to the trash/recycle bin. Example: A basic program to delete the file from a static path.Jav 2 min read Like