- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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.
- Related Articles
- How to move a file from one folder to another using Python?
- How to read data from one file and print to another file in Java?
- How to write a program to copy characters from one file to another in Java?
- Moving objects from one package to another package in SAP HANA
- How to create a new directory by using File object in Java?
- Java Program to rename a file or directory
- How to import classes from within another directory/package in Java?
- Java Program to delete file or directory
- Golang program to copy one file into another file
- How to search a file in a directory in java
- Java Program to construct one String from another
- Java Program to Call One Constructor from another
- Is it possible to change directory by using File object in Java?
- How to list all files (only) from a directory using Java?
- How to search a directory with file extensions in Java?

Advertisements