In this source code example, we show you how to move or rename a file to a target file in Java.
We use Files.move() method to move or rename a file to a target file.
Java
Java IO
Java NIO
Java File Move Example
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class JavaMoveFile {
public static void main(String[] args) throws IOException {
Path myPath = Paths.get("src/resources/myfile.txt");
Path myPath2 = Paths.get("src/resources/myfile2.txt");
Files.move(myPath, myPath2);
System.out.println("File moved");
}
}
References
https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/nio/file/Files.html
Related Java NIO Files Examples
- Java File Copy Example
- Java Create File Example
- Java Delete File Example
- Java File Size Example
- Java Get File Owner Example
- Java File Move Example
- Java Read File Example
- Java Write File Example
- Java Create Directory Example
- Java Create Multiple Directories Example
- Java List All Subdirectories From a Directory Example
- Java Append to File Example
Comments
Post a Comment