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 program to illustrate renaming and// moving a file permanently to a new loactionimportjava.io.*;importjava.nio.file.Files;importjava.nio.file.*;publicclassTest{publicstaticvoidmain(String[] args)throwsIOException{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");}}}chevron_rightfilter_noneOutput:
File renamed and moved successfully
- Using 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 program to illustrate Copying the file// and deleting the original fileimportjava.io.*;publicclassTest{publicstaticvoidmain(String[] args){File file =newFile("C:\\Users\\Mayank\\Desktop\\1.txt");// renaming the file and moving it to a new locationif(file.renameTo(newFile("C:\\Users\\Mayank\\Desktop\\dest\\newFile.txt"))){// if file copied successfully then delete the original filefile.delete();System.out.println("File moved successfully");}else{System.out.println("Failed to move the file");}}}chevron_rightfilter_noneOutput
File moved successfully
References:
This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Different Ways to Copy Content From One File to Another File in Java
- How to run java class file which is in different directory?
- Java Program to Search for a File in a Directory
- Traversing directory in Java using BFS
- How to find and open the Hidden files in a Directory using Java
- Java program to List all files in a directory and nested sub-directories | Recursive approach
- Java program to merge contents of all the files in a directory
- Java Program to Traverse in a Directory
- Java Program to Check if a Directory is Empty or Not
- Java Program to Get the Size of a Directory
- Java Program to Find Current Working Directory
- Copying file using FileStreams in Java
- File handling in Java using FileWriter and FileReader
- Delete a file using Java
- How to play an Audio file using Java
- Counting number of lines, words, characters and paragraphs in a text file using Java
- Size of file on the Internet using Java
- Creating a Cell at specific position in Excel file using Java
- Reading a CSV file in Java using OpenCSV
- Writing a CSV file in Java using OpenCSV

