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.
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); Files.move(source, target); Files.deleteIfExists(target);
Try it
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.
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
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
copyon a directory creates only an empty directory, not its contents. Copy a tree withFiles.walkand copy each entry.moveis 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
moveit onto the target withATOMIC_MOVE(andREPLACE_EXISTINGwhere supported). existsand race conditions. Checking then acting is not safe if other processes touch the files; prefer to attempt the operation and catch the exception.existsreturnsfalseon errors (for example when you lack permission to see the file), sofalsedoes not always mean "absent".notExistsis not just the negation either: both can befalsewhen the answer is unknown.deleteon a symbolic link removes the link, not its target.sizeis 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.
Related
Files.writeString(),Files.walk(),Files.createTempFile(),Path,File- Lessons: Files