Home » Tip Bank » Java
  • Comment on this Article
Language: Java
Expertise: Intermediate
Sep 20, 2019
  • facebook
  • twitter
  • in
  • Email

WEBINAR:

On-Demand

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


Watch

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
 
Submit a Tip
Browse "Java" Tips
Browse All Tips
html-color-codes.info
w3schools.com