java

How to move a file from one directory to another in Java

In this tutorial, we are going to see how to move a file from one directory to another in Java. The move() method located in the Files.Path class: renames and permanently moves the file to a new location.
 

How to move a file from one directory to another in Java
import java.io.*; 
import java.nio.file.Files; 
import java.nio.file.*; 

public class Main 
{ 
    public static void main(String[] args) throws IOException 
    { 
        String src = "C:\\Users\\PC\\Desktop\\bb.txt";
        String dest = "C:\\Users\\PC\\Desktop\\Folder\\bb.txt";
        Path tmp = Files.move(Paths.get(src), Paths.get(dest)); 
  
        if(tmp != null) 
        { 
            System.out.println("File successfully moved"); 
        } 
        else
        {
            System.out.println("Unable to move the file"); 
        } 
    } 
}

Output:

File successfully moved
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *