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 move a file or directory by using the 您可以使用move(Path, Path, CopyOption...)
method. move(Path, Path, CopyOption...)
方法移动文件或目录。The move fails if the target file exists, unless the 如果目标文件存在,则移动失败,除非指定了REPLACE_EXISTING
option is specified.REPLACE_EXISTING
选项。
Empty directories can be moved. 可以移动空目录。If the directory is not empty, the move is allowed when the directory can be moved without moving the contents of that directory. 如果目录不为空,则当可以移动目录而不移动该目录的内容时,允许移动。On UNIX systems, moving a directory within the same partition generally consists of renaming the directory. 在UNIX系统上,在同一分区内移动目录通常包括重命名目录。In that situation, this method works even when the directory contains files.在这种情况下,即使目录中包含文件,此方法也可以工作。
This method takes a varargs argument – the following 此方法采用varargs参数–支持以下StandardCopyOption
enums are supported:StandardCopyOption
枚举:
REPLACE_EXISTING
– ATOMIC_MOVE
– ATOMIC_MOVE
you can move a file into a directory and be guaranteed that any process watching the directory accesses a complete file.ATOMIC_MOVE
,您可以将文件移动到目录中,并保证任何监视目录的进程都可以访问完整的文件。The following shows how to use the 下面显示了如何使用move
method:move
方法:
import static java.nio.file.StandardCopyOption.*; ... Files.move(source, target, REPLACE_EXISTING);
Though you can implement the 尽管可以在单个目录上实现move
method on a single directory as shown, the method is most often used with the file tree recursion mechanism. move
方法,如图所示,但该方法最常用于文件树递归机制。For more information, see Walking the File Tree.有关详细信息,请参阅遍历文件树。