The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
You can copy a file or directory by using the 您可以使用copy(Path, Path, CopyOption...)
method. copy(Path, Path, CopyOption...)
方法复制文件或目录。The copy fails if the target file exists, unless the 如果目标文件存在,则复制失败,除非指定了REPLACE_EXISTING
option is specified.REPLACE_EXISTING
选项。
Directories can be copied. 可以复制目录。However, files inside the directory are not copied, so the new directory is empty even when the original directory contains files.但是,目录中的文件不会被复制,因此即使原始目录包含文件,新目录也是空的。
When copying a symbolic link, the target of the link is copied. 复制符号链接时,将复制链接的目标。If you want to copy the link itself, and not the contents of the link, specify either the 如果要复制链接本身而不是链接的内容,请指定NOFOLLOW_LINKS
or REPLACE_EXISTING
option.NOFOLLOW_LINKS
或REPLACE_EXISTING
选项。
This method takes a varargs argument. 此方法采用varargs参数。The following 支持以下StandardCopyOption
and LinkOption
enums are supported:StandardCopyOption
和LinkOption
枚举:
REPLACE_EXISTING
– DirectoryNotEmptyException
exception.DirectoryNotEmptyException
异常。COPY_ATTRIBUTES
– last-modified-time
is supported across platforms and is copied to the target file.last-modified-time
,并将其复制到目标文件。NOFOLLOW_LINKS
– If you are not familiar with 如果不熟悉enums
, see Enum Types.enums
,请参阅枚举类型。
The following shows how to use the 下面显示了如何使用copy
method:copy
方法:
import static java.nio.file.StandardCopyOption.*; ... Files.copy(source, target, REPLACE_EXISTING);
In addition to file copy, the 除了文件复制之外,Files
class also defines methods that may be used to copy between a file and a stream. Files
类还定义了可用于在文件和流之间复制的方法。The copy(InputStream, Path, CopyOptions...)
method may be used to copy all bytes from an input stream to a file. copy(InputStream, Path, CopyOptions...)
方法可用于将输入流中的所有字节复制到文件中。The copy(Path, OutputStream)
method may be used to copy all bytes from a file to an output stream.copy(Path, OutputStream)
方法可用于将文件中的所有字节复制到输出流。
The Copy
example uses the copy
and Files.walkFileTree
methods to support a recursive copy. Copy
示例使用copy
和Files.walkFileTree
方法支持递归复制。See Walking the File Tree for more information.有关详细信息,请参阅遍历文件树。