Move a file: ========================================================= https://mkyong.com/java/how-to-move-file-to-another-directory-in-java/ FileMove.java package com.mkyong.io.file; import java.io.IOException; import java.nio.file.*; public class FileMove { public static void main(String[] args) { String fromFile = "/home/mkyong/data/db.debug.conf"; String toFile = "/home/mkyong/data/deploy/db.conf"; Path source = Paths.get(fromFile); Path target = Paths.get(toFile); try { // rename or move a file to other path // if target exists, throws FileAlreadyExistsException Files.move(source, target); // if target exists, replace it. // Files.move(source, target, StandardCopyOption.REPLACE_EXISTING); // multiple CopyOption /*CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES, LinkOption.NOFOLLOW_LINKS }; Files.move(source, target, options);*/ } catch (IOException e) { e.printStackTrace(); } } } ===========================Rename/Move a file (to a new directory)======================= https://mkyong.com/java/how-to-rename-file-in-java/ In Java, we can use the NIO Files.move(source, target) to rename or move a file. import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; //... Path source = Paths.get("/home/mkyong/newfolder/test1.txt"); Path target = Paths.get("/home/mkyong/newfolder/test2.txt"); try{ Files.move(source, target); } catch (IOException e) { e.printStackTrace(); } =========================================================== https://www.geeksforgeeks.org/moving-file-one-directory-another-using-java/ ======================Move a file to a new directory:=================== // 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"); } } } ======================Move a file to a new directory AND DELETE FROM ORIGINAL DIR:=================== / 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 ============================================================================ 2. delete files https://www.geeksforgeeks.org/files-delete-method-in-java-with-examples/?ref=ml_lbp ========== delete a file =============================== // Java program to demonstrate // java.nio.file.Files.delete() method import java.io.IOException; import java.nio.file.*; public class GFG { public static void main(String[] args) { // create object of Path Path path = Paths.get("D:\\Work\\Test\\file1.txt"); // delete File try { Files.delete(path); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ========== delete two files in folder ====================== // Java program to demonstrate // java.nio.file.Files.delete() method import java.io.IOException; import java.nio.file.*; public class GFG { public static void main(String[] args) { // create object of Path Path pathOfFile1 = Paths.get("D:\\temp\\Files" + "\\Cover Letter.docx"); Path pathOfFile2 = Paths.get("D:\\temp\\Files" + "\\Java-Concurrency-Essentials.pdf"); // delete both Files try { Files.delete(pathOfFile1); Files.delete(pathOfFile2); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }