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发行说明。
Prior to the Java SE 7 release, the 在Java SE 7发布之前,java.io.File
class was the mechanism used for file I/O, but it had several drawbacks.java.io.File
类是用于文件I/O的机制,但它有几个缺点。
rename
method didn't work consistently across platforms.rename
方法在不同平台上的工作不一致。File
methods didn't scale. File
方法无法缩放。Perhaps you have legacy code that uses 也许您有使用java.io.File
and would like to take advantage of the java.nio.file.Path
functionality with minimal impact to your code.java.io.File
的遗留代码,并且希望在对代码影响最小的情况下利用java.nio.file.Path
功能。
The java.io.File
class provides the toPath
method, which converts an old style File
instance to a java.nio.file.Path
instance, as follows:java.io.File
类提供 toPath
方法,该方法将旧式文件实例转换为java.nio.File.Path
实例,如下所示:
Path input = file.toPath();
You can then take advantage of the rich feature set available to the 然后可以利用Path
class.Path
类可用的丰富功能集。
For example, assume you had some code that deleted a file:例如,假设您有一些删除文件的代码:
file.delete();
You could modify this code to use the 您可以修改此代码以使用Files.delete
method, as follows:Files.delete
方法,如下所示:
Path fp = file.toPath(); Files.delete(fp);
Conversely, the 相反,Path.toFile
method constructs a java.io.File
object for a Path
object.Path.toFile
方法为Path
对象构造java.io.File
对象。
Because the Java implementation of file I/O has been completely re-architected in the Java SE 7 release, you cannot swap one method for another method. 由于文件I/O的Java实现在JavaSE7版本中已经完全重新设计,因此不能将一种方法替换为另一种方法。If you want to use the rich functionality offered by the 如果您想使用java.nio.file
package, your easiest solution is to use the File.toPath
method as suggested in the previous section. java.nio.file
包提供的丰富功能,最简单的解决方案是使用上一节中建议的File.toPath
方法。However, if you do not want to use that approach or it is not sufficient for your needs, you must rewrite your file I/O code.但是,如果您不想使用这种方法,或者它不足以满足您的需要,则必须重写文件I/O代码。
There is no one-to-one correspondence between the two APIs, but the following table gives you a general idea of what functionality in the 这两个API之间没有一对一的对应关系,但下表让您大致了解java.io.File
API maps to in the java.nio.file
API and tells you where you can obtain more information.java.io.File
API中的哪些功能映射到java.nio.File
API中,并告诉您可以从哪里获得更多信息。
java.io.File |
java.nio.file |
|
---|---|---|
java.io.File |
java.nio.file.Path |
|
java.io.RandomAccessFile |
SeekableByteChannel functionality.SeekableByteChannel 功能。 |
|
File.canRead , canWrite , canExecute |
Files.isReadable , Files.isWritable , and Files.isExecutable . |
|
File.isDirectory() , File.isFile() , and File.length() |
Files.isDirectory(Path, LinkOption...) , Files.isRegularFile(Path, LinkOption...) , and Files.size(Path) |
|
File.lastModified() and File.setLastModified(long) |
Files.getLastModifiedTime(Path, LinkOption...) and Files.setLastMOdifiedTime(Path, FileTime) |
|
The File methods that set various attributes: setExecutable , setReadable , setReadOnly , setWritable |
Files method setAttribute(Path, String, Object, LinkOption...) .Files 方法setAttribute(Path, String, Object, LinkOption...) 替换。 |
|
new File(parent, "newfile") |
parent.resolve("newfile") |
|
File.renameTo |
Files.move |
|
File.delete |
Files.delete |
|
File.createNewFile |
Files.createFile |
|
File.deleteOnExit |
DELETE_ON_CLOSE option specified in the createFile method.createFile 方法中指定的DELETE_ON_CLOSE 选项。 |
|
File.createTempFile |
Files.createTempFile(Path, String, FileAttributes<?>) , Files.createTempFile(Path, String, String, FileAttributes<?>) |
|
File.exists |
Files.exists and Files.notExists |
|
File.compareTo and equals |
Path.compareTo and equals |
|
File.getAbsolutePath and getAbsoluteFile |
Path.toAbsolutePath |
|
File.getCanonicalPath and getCanonicalFile |
Path.toRealPath or normalize |
toRealPath )normalize ) |
File.toURI |
Path.toURI |
|
File.isHidden |
Files.isHidden |
|
File.list and listFiles |
Path.newDirectoryStream |
|
File.mkdir and mkdirs |
Files.createDirectory |
|
File.listRoots |
FileSystem.getRootDirectories |
|
File.getTotalSpace , File.getFreeSpace , File.getUsableSpace |
FileStore.getTotalSpace , FileStore.getUnallocatedSpace , FileStore.getUsableSpace , FileStore.getTotalSpace |