dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Sep 20, 2019

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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 FileMove
Moved sourceFiles to targetFiles

*/ 
M S Sridhar
 
Thanks for your registration, follow us on our social networks to keep up-to-date