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*/ 

devx-admin

Share the Post: