In this quick tutorial, we will learn different ways to move a file in Java.
1. Move File using File.renameTo() Method
- Create a file named "sample.txt" in the directory "C:/workspace".
- Create File class object by passing file absolute location path "C:/workspace/sample.txt".
- We need to pass the new abstract pathname to renameTo() method to move the file.
- renameTo() method returns true if and only if the renaming succeeded; false otherwise.
- Observe the directory whether the file is moved or not.
import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This Java program demonstrates how to move file in Java.
* @author javaguides.net
*/
public class MoveFileExample {
private static final Logger LOGGER = LoggerFactory.getLogger(MoveFileExample.class);
public static void main(String[] args) {
moveFile();
}
public static void moveFile() {
File file = new File("C:/workspace/sample.txt");
boolean move = file.renameTo(new File("C:/workspace/moved/sample.txt"));
if (move) {
LOGGER.info("File is moved successful!");
} else {
LOGGER.info("File is failed to move!");
}
}
}
2. Move File using Files.move() Method
java.nio.file.Files provide several static methods that operate on files, directories, or other types of files. To move a file to a target file, we can use its move() method. The implementation is platform-independent, but it doesn’t fail if the file attributes are not copied. Javadoc guarantees only the last modified time to be copied to the new file.This method takes the path to the file to move, the path to the target file, and an optional parameter to specify how the move is performed. By default, moving fails if the target file already exists unless the REPLACE_EXISTING option is specified. If the source and target are the same files, the method completes without moving the file. If either the source file or the destination directory does not exist, java.nio.file.NoSuchFileException is thrown.
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
public class Main
{
public static void main(String[] args)
{
File from = new File("src.txt");
File to = new File("dest.txt");
try {
Files.move(from.toPath(), to.toPath(), StandardCopyOption.REPLACE_EXISTING);
System.out.println("File moved successfully.");
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
Reference
Related Java IO Tutorials
How to create a new file in java
How to Write File in Java
How to delete a file in Java
How to Copy File in Java
How to Move a File in Java
How to Rename File in Java
How to Append to File in Java
Read File using BufferReader
Read File using BufferInputStream
Read File using DataInputStream
Get File Size in Bytes KB MB GB TB
How to write an Object to file in Java
How to Read Object from File
How to Write File in Java
How to delete a file in Java
How to Copy File in Java
How to Move a File in Java
How to Rename File in Java
How to Append to File in Java
Read File using BufferReader
Read File using BufferInputStream
Read File using DataInputStream
Get File Size in Bytes KB MB GB TB
How to write an Object to file in Java
How to Read Object from File
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course
Comments
Post a Comment