Close

Java IO & NIO - Files.move() Examples

Java IO & NIO Java Java API 


Class:

java.nio.file.Files

java.lang.Objectjava.lang.Objectjava.nio.file.Filesjava.nio.file.FilesLogicBig

Method:

public static Path move(Path source,
                        Path target,
                        CopyOption... options)
                 throws IOException

Move or rename a file to a target file.

Parameters:
source - the path to the file to move
target - the path to the target file (may be associated with a different provider to the source path)
options - options specifying how the move should be done
Returns:
the path to the target file

Examples


Moving a Single File

package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class MoveExample {

public static void main(String... args) throws IOException {
Path fileToMovePath = Files.createTempFile(Files.createTempDirectory("testDir1"),
"test-file", ".txt");
System.out.println("File to move: " + fileToMovePath);
System.out.println("File to move exits: " + Files.exists(fileToMovePath));

Path destinationDir = Files.createTempDirectory("testDir2");
Path path = Files.move(fileToMovePath, destinationDir.resolve(fileToMovePath.getFileName()),
StandardCopyOption.REPLACE_EXISTING);
System.out.println("File moved to: " + path);
System.out.println("File moved exits: " + Files.exists(path));
System.out.println("File to move exits: " + Files.exists(fileToMovePath));
}
}

Output

File to move: C:\Users\joe\AppData\Local\Temp\testDir16128654961899909640\test-file6059498061581786860.txt
File to move exits: true
File moved to: C:\Users\joe\AppData\Local\Temp\testDir21848676815077755568\test-file6059498061581786860.txt
File moved exits: true
File to move exits: false




Renaming a file

package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class MoveExample2 {

public static void main(String... args) throws IOException {
Path fileToRenamePath = Files.createTempFile(Files.createTempDirectory("testDir1"),
"test-file", ".txt");
System.out.println("File to rename: " + fileToRenamePath);
System.out.println("File to rename exits: " + Files.exists(fileToRenamePath));

Path path = Files.move(fileToRenamePath, fileToRenamePath.getParent().resolve("my-renamed-file.txt"),
StandardCopyOption.REPLACE_EXISTING);
System.out.println("File renamed to: " + path);
System.out.println("File renamed exits: " + Files.exists(path));
System.out.println("File to rename exits: " + Files.exists(fileToRenamePath));
}
}

Output

File to rename: C:\Users\joe\AppData\Local\Temp\testDir15053553132436423466\test-file4166835605694469519.txt
File to rename exits: true
File renamed to: C:\Users\joe\AppData\Local\Temp\testDir15053553132436423466\my-renamed-file.txt
File renamed exits: true
File to rename exits: false




Moving a Directory with content

package com.logicbig.example.files;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class MoveExample3 {

public static void main(String... args) throws IOException {
Path srcDir = Files.createTempDirectory("source-directory");
Files.createTempFile(srcDir, "test-file1", ".txt");
Files.createTempFile(srcDir, "test-file2", ".txt");
Path subDir = Files.createTempDirectory(srcDir, "test-temp-directory");
Files.createTempFile(subDir, "test-sub-file", ".txt");

System.out.println("-- source dir --");
printDirectory(srcDir.toFile(), 0);

Path destinationDir = Files.createTempDirectory("test-directory2").resolve("destination-directory");

Files.move(srcDir, destinationDir);

System.out.println("\n-- destination dir --");
printDirectory(destinationDir.toFile(), 0);

System.out.println("\n-- source dir after move --");
printDirectory(srcDir.toFile(), 0);

}

private static void printDirectory(File dir, int level) {
printName(dir.getName(), level);
File[] files = dir.listFiles();
if (files == null) {
return;
}
for (File file : files) {
if (file.isDirectory()) {
printDirectory(file, level + 1);
} else {
printName(file.getName(), level + 1);
}
}
}

private static void printName(String name, int level) {
String indentation = "";
for (int i = 0; i < level; i++) {
indentation += "|\t";
}
System.out.println(indentation + "|---" + name);
}
}

Output

-- source dir --
|---source-directory952447878473554881
| |---test-file13719419387120267988.txt
| |---test-file22265021341524486393.txt
| |---test-temp-directory3420697905790138055
| | |---test-sub-file6374405921325505324.txt

-- destination dir --
|---destination-directory
| |---test-file13719419387120267988.txt
| |---test-file22265021341524486393.txt
| |---test-temp-directory3420697905790138055
| | |---test-sub-file6374405921325505324.txt

-- source dir after move --
|---source-directory952447878473554881




See Also