Java java.nio.file.Files copy() Method

Java File Java java.nio.file.Files


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 it
  • StandardCopyOption.COPY_ATTRIBUTES: Copy file attributes to the new file
  • LinkOption.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());
}

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());
}

Notes

  1. Directory copy: Files.copy()The method does not recursively copy directory contents by default; it only copies empty directories
  2. Symbolic links: Follow symbolic links by default, unless specifiedLinkOption.NOFOLLOW_LINKS
  3. Atomicity: File copy operations are not atomic and may fail during the operation
  4. Performance: For large files, NIO copy is generally more efficient than traditional IO
  5. Exception handling: Must handle exceptions that may be thrownIOException

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

  1. For simple file copying, use the most basicFiles.copy()form
  2. When file attributes need to be preserved, add theCOPY_ATTRIBUTESoption
  3. When handling a target file that may already exist, useREPLACE_EXISTING
  4. For large files, consider usingFiles.copy()instead of traditional IO methods
  5. Always use try-with-resources to handle stream resources

Java File Java java.nio.file.Files

Other Extensions