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, used to copy files or directories between file systems. This method provides, compared with the traditionaljava.iopackage, more efficient and flexible file operation capabilities.
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 usedCopyOptioninclude:
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());
}
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());
}
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());
}
Copying from Input Stream to File
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());
}
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: Follow symbolic links by default, unless specified
LinkOption.NOFOLLOW_LINKS - Atomicity: File copy operations are not atomic and may fail during the operation
- Performance: For large files, NIO copy is generally more efficient than traditional IO
- Exception handling: Must handle exceptions that may be thrown
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 the
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