Java java.nio.file.Files copy() Method
java.nio.file.Files.copy() is a utility method provided in the Java NIO (New Input/Output) package for copying files or directories between file systems. This method provides more than traditional java.io package for efficient and flexible file operations.
Method overloading
Files.copy() The method has three main overloaded forms:
Copy from input stream to file
public
static
long
copy
(
InputStream
in, Path target, CopyOption...
options
)
throws
IOException
Copy from file to output stream
public
static
long
copy
(
Path source,
OutputStream
out
)
throws
IOException
File-to-file copy
public
static
Path copy
(
Path source, Path target, CopyOption...
options
)
throws
IOException
Parameter details
Input parameters
- InputStream in: The input stream to copy
- Path source: The source file path
- Path target: The target file path
- OutputStream out: The output stream to write to
- CopyOption... options: Optional copy options
CopyOption options
Commonly used CopyOption include:
StandardCopyOption.REPLACE_EXISTING: If the target file already exists, replace itStandardCopyOption.COPY_ATTRIBUTES: Copy file attributes to the new fileLinkOption.NOFOLLOW_LINKS: Do not follow symbolic links
Return Value
- For stream-to-file copy: returns the number of bytes copied
- For file-to-file copy: returns the target file path
- For file-to-stream copy: returns the number of bytes copied
Usage examples
Basic file copy
Example
Path source
=
Paths.
get
(
"source.txt"
)
;
Path target
=
Paths.
get
(
"target.txt"
)
;
try
{
Files.
copy
(
source, target, StandardCopyOption.
REPLACE_EXISTING
)
;
System
.
out
.
println
(
"File copied successfully"
)
;
}
catch
(
IOException
e
)
{
System
.
err
.
println
(
"Copy failed: "
e.
getMessage
(
)
)
;
}
File copy with attributes
Example
Path source
=
Paths.
get
(
"source.txt"
)
;
Path target
=
Paths.
get
(
"target.txt"
)
;
try
{
Files.
copy
(
source, target,
StandardCopyOption.
REPLACE_EXISTING
,
StandardCopyOption.
COPY_ATTRIBUTES
)
;
System
.
out
.
println
(
"File and attributes copied successfully"
)
;
}
catch
(
IOException
e
)
{
System
.
err
.
println
(
"Copy failed: "
e.
getMessage
(
)
)
;
}
Input stream to file copy
Example
try
(
InputStream
in
=
new
URL
(
"http://example.com/file.txt"
)
.
openStream
(
)
)
{
Path target
=
Paths.
get
(
"downloaded.txt"
)
;
long
bytesCopied
=
Files.
copy
(
in, target
)
;
System
.
out
.
println
(
"Download complete, copied "
bytesCopied
" bytes"
)
;
}
catch
(
IOException
e
)
{
System
.
err
.
println
(
"Download failed: "
e.
getMessage
(
)
)
;
}
Notes
- Directory copy:
Files.copy()The method does not recursively copy directory contents by default; it only copies empty directories - Symbolic links: Follows symbolic links by default, unless specified
LinkOption.NOFOLLOW_LINKS - Atomicity: The file copy operation is not atomic and may fail during the operation
- Performance: For large files, NIO copy is usually more efficient than traditional IO
- Exception handling: You must handle the possible
IOException
Comparison with traditional IO
| Feature | NIO Files.copy() | Traditional IO (FileInputStream/FileOutputStream) |
|---|---|---|
| Performance | Higher | Lower |
| Feature richness | Richer | Basic |
| Exception handling | More concise | More cumbersome |
| Symbolic link handling | Supported | Not supported |
| File attribute copy | Supported | Not supported |
Best Practices
- For simple file copying, use the most basic
Files.copy()form - When file attributes need to be preserved, add
COPY_ATTRIBUTESOption - When handling a target file that may already exist, use
REPLACE_EXISTING - For large files, consider using
Files.copy()instead of traditional IO methods - Always use try-with-resources to handle stream resources
Java java.nio.file.Files