Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท I/O and Files

Files.copy(), move() and delete()

Reference for java.nio.file.Files file operations: copy, move, delete, deleteIfExists, exists, size, createDirectories and the REPLACE_EXISTING option.

Besides reading and writing, the Files class manages files themselves. Files.copy(source, target) duplicates a file, Files.move(source, target) renames or relocates it, and Files.delete(path) removes it. Query methods such as exists, isDirectory, isRegularFile and size, plus createDirectory and createDirectories, round out the set.

Unlike the old File.renameTo and File.delete, which return false when something goes wrong, these methods throw a specific IOException subclass that tells you why.

Java
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
Files.move(source, target);
Files.deleteIfExists(target);

Try it

Java Copy, move and delete

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Directories, checks and streams

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
static Path copy(Path source, Path target, CopyOption... options) throws IOException
static long copy(InputStream in, Path target, CopyOption... options) throws IOException
static long copy(Path source, OutputStream out) throws IOException
static Path move(Path source, Path target, CopyOption... options) throws IOException
static void delete(Path path) throws IOException
static boolean deleteIfExists(Path path) throws IOException
static boolean exists(Path path, LinkOption... options)
static long size(Path path) throws IOException
static Path createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException
static Path createDirectories(Path dir, FileAttribute<?>... attrs) throws IOException
Option Meaning
StandardCopyOption.REPLACE_EXISTING Overwrite an existing target instead of failing.
StandardCopyOption.COPY_ATTRIBUTES Also copy the timestamps and other attributes where supported.
StandardCopyOption.ATOMIC_MOVE Move as one atomic step, or fail with AtomicMoveNotSupportedException.
LinkOption.NOFOLLOW_LINKS Treat a symbolic link as the link itself.

Returns: the target path (path-based copy/move), the number of bytes copied (stream-based copy), true if deleteIfExists removed something.

Throws: NoSuchFileException, FileAlreadyExistsException, DirectoryNotEmptyException, AccessDeniedException, all subclasses of IOException.

Usage notes

  • copy on a directory creates only an empty directory, not its contents. Copy a tree with Files.walk and copy each entry.
  • move is a rename within one file system, which is fast and can be atomic. Across file systems it falls back to copy and delete, and a non-empty directory can then not be moved.
  • Safe replacement: write a temporary file in the same directory, then move it onto the target with ATOMIC_MOVE (and REPLACE_EXISTING where supported).
  • exists and race conditions. Checking then acting is not safe if other processes touch the files; prefer to attempt the operation and catch the exception.
  • exists returns false on errors (for example when you lack permission to see the file), so false does not always mean "absent". notExists is not just the negation either: both can be false when the answer is unknown.
  • delete on a symbolic link removes the link, not its target.
  • size is in bytes; for a directory the result is platform dependent.
  • Copying an existing file onto itself (the same file) does nothing, and it does not fail.