Files.copy() / move() / list() / walk()
Methods for copying, moving, renaming files, and listing the contents of a directory. Using methods from the Files class lets you perform file operations more intuitively than the traditional File class.
Syntax
// Copy a file.
Files.copy(sourcePath, destinationPath);
// Copy a file, allowing overwrite.
Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
// Move (or rename) a file.
Files.move(sourcePath, destinationPath);
// Get a list of files and directories directly under a directory.
Stream<Path> entries = Files.list(Path.of("directoryPath"));
// Recursively traverse a directory tree.
Stream<Path> all = Files.walk(Path.of("directoryPath"));
Method List
| Method | Description |
|---|---|
| Files.copy(src, dst) | Copies a file. Throws an exception if the destination already exists. |
| Files.copy(src, dst, options) | Specifying StandardCopyOption.REPLACE_EXISTING allows the destination to be overwritten. |
| Files.move(src, dst) | Moves or renames a file. The operation is atomic when performed within the same file system. |
| Files.list(dir) | Returns the entries directly under a directory as a Stream<Path>. Does not recurse into subdirectories. |
| Files.walk(start) | Returns a Stream<Path> by recursively traversing the specified directory. You can limit the depth with a second argument. |
| Files.find(start, depth, BiPredicate) | Recursively searches for paths that match a condition and returns them as a Stream<Path>. |
Sample Code
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
// Copy a file (with overwrite enabled).
try {
Files.copy(
Path.of("original.txt"),
Path.of("backup.txt"),
StandardCopyOption.REPLACE_EXISTING
);
System.out.println("Copy complete.");
} catch (IOException e) {
e.printStackTrace();
}
// Move (rename) a file.
try {
Files.move(
Path.of("old_name.txt"),
Path.of("new_name.txt"),
StandardCopyOption.REPLACE_EXISTING
);
System.out.println("Move complete.");
} catch (IOException e) {
e.printStackTrace();
}
// List files directly under a directory.
try (var stream = Files.list(Path.of("."))) {
stream.forEach(p -> System.out.println(p.getFileName()));
} catch (IOException e) {
e.printStackTrace();
}
// Recursively traverse a directory and print only .txt files.
try (var stream = Files.walk(Path.of("."))) {
stream
.filter(p -> p.toString().endsWith(".txt"))
.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
Notes
Both Files.list() and Files.walk() return a Stream, so always use them inside a try-with-resources block. Failing to close the stream will leak file handles.
To delete a directory and all its contents using Files.walk(), you must delete entries starting from the deepest level. Combining it with sorted(Comparator.reverseOrder()) ensures subdirectories are deleted before their parents (e.g., walk(dir).sorted(Comparator.reverseOrder()).forEach(Files::delete)).
For checking file existence and creation, see Files.exists() / isFile() / isDirectory().
If you find any errors or copyright issues, please contact us.