 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Moving a file from one directory to another using Java
We can use Files.move() API to move file from one directory to another. Following is the syntax of the move method.
public static Path move(Path source,Path target,CopyOption... options) throws IOException
Where
- source − Source path of file to be moved 
- target − Target path of file to be moved 
- options − options like REPLACE_EXISTING, ATOMIC_MOVE 
Example
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class Tester {    public static void main(String[] args) {       //move file from D:/temp/test.txt to D:/temp1/test.txt       //make sure that temp1 folder exists       moveFile("D:/temp/test.txt", "D:/temp1/test.txt");    }    private static void moveFile(String src, String dest ) {       Path result = null;       try {          result = Files.move(Paths.get(src), Paths.get(dest));       } catch (IOException e) {          System.out.println("Exception while moving file: " + e.getMessage());       }       if(result != null) {          System.out.println("File moved successfully.");       }else{          System.out.println("File movement failed.");       }    } }  Output
File moved successfully.
Advertisements
 