devxlogo

Moving a Folder in Java

Moving a Folder in Java

The move method accepts three arguments. The 1st and 2nd being the source and target Path attributes and the 3rd being the replace option.

import java.nio.file.*;import static java.nio.file.StandardCopyOption.*;public class FileMove{   public static void main(String args[])   {      FileMove fileMove = new FileMove();      fileMove.proceed();   }      private void proceed()   {      try{         //Create a folder named sourceFiles. We will now move them to targetFiles.         Path source = Paths.get("/opt/myfiles/sourceFiles");         Path target = Paths.get("/opt/myfiles/targetFiles");         Files.move(source, target, REPLACE_EXISTING);            }catch(Exception ioe)      {         System.out.println("Exception: " + ioe);      }   }}/*

Expected output:

[root@mypc]# java FileMoveMoved sourceFiles to targetFiles*/ 
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist