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


advertisement
 

Tip: Moving a Folder in Java

Learn how to employ the three arguments used in the move method in Java.


advertisement

WEBINAR:

On-Demand

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


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

*/ 

 

Visit the DevX Tip Bank

 





   
Sridhar M S. is a Java developer from Bangalore, India. He holds a master's degree in Computer Science.
Thanks for your registration, follow us on our social networks to keep up-to-date