Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

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

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 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

CopyOption options

Commonly used CopyOption include:

Return Value


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

  1. Directory copy: Files.copy() The method does not recursively copy directory contents by default; it only copies empty directories
  2. Symbolic links: Follows symbolic links by default, unless specified LinkOption.NOFOLLOW_LINKS
  3. Atomicity: The file copy operation is not atomic and may fail during the operation
  4. Performance: For large files, NIO copy is usually more efficient than traditional IO
  5. 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

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

Java File Java java.nio.file.Files