A moving a file programmatically seems a tough task but it’s not. You have just need to knowledge about Java File Handling and basic controls statements in java. Some application has a need to move single or multiple files (directory) to another place in the application for better management. In this tutorial, you will learn about How to Java Move File programmatically with examples.
Java provides a method to Move files. It could be an entire file or files inside a directory. Before reading this tutorial we suggest reading a how-to Create, Write and Delete tutorial of the java file.
A Ways to Move File in Java
- Files.Path move() method
- Using 2 methods – Java.io.File.renameTo() and Java.io.File.delete() methods:
Let’s start Java move file examples
# Using a Files.Path move() method
A standard move() method using NIO, where you need the pass a source file name and destination of the
Syntax
Need to import a java.nio package.
import static java.nio.file.StandardCopyOption.*; ... Files.move(source, target, REPLACE_EXISTING);
Simple Example
How to move a file from one folder to another in java example code.
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class Hello { public static void main(String arg[]){ try { Files.move(Paths.get("test.txt"), Paths.get("dest/test.txt")); System.out.println("Successfully moved file"); } catch (IOException e) { e.printStackTrace(); } } }
Output: A GIF output for how program has worked.
#Using a renameTo() and delete() java File methods
In this way, you have to copy a file and delete the original file. For that, there will 2 methods involved Java.io.File.renameTo() and Java.io.File.delete().
Complete Example
Here we used an if-else condition statement, where the file will delete only if the file renamed else messages will print failed.
import java.io.File; public class Hello { public static void main(String arg[]) { File file = new File("test.txt"); // renaming the file and it will move new location if (file.renameTo (new File("dest/newFile.txt"))) { // delete the original file file.delete(); System.out.println("File moved successfully"); } else { System.out.println("File failed to move"); } } }
Output: File moved successfully
How did it work?
Here is diagram for upper example how project structure and file moved one directory to another.
Question: How Java move all files from one directory to another?
Answer: First check its directory or not then count the number of the file in the directory. Run the java for loop and move all files.
File file = new File("C:\\Users\\eye\\Desktop\\Test");
if(file.isDirectory()) {
File[] content = file.listFiles();
for(int i = 0; i < content.length; i++) {
//move content[i]
}
}
Task: How to java move file to another directory overwrite?
Answer: do solve this question and write in a comment. We will update a
Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All Java Move File are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.|