Documentation

The Java™ Tutorials
Hide TOC
Legacy File I/O Code遗留文件I/O代码
Trail: Essential Java Classes
Lesson: Basic I/O
Section: File I/O (Featuring NIO.2)

Legacy File I/O Code遗留文件I/O代码

Interoperability With Legacy Code与遗留代码的互操作性

Prior to the Java SE 7 release, the java.io.File class was the mechanism used for file I/O, but it had several drawbacks.在Java SE 7发布之前,java.io.File类是用于文件I/O的机制,但它有几个缺点。

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对象。

Mapping java.io.File Functionality to java.nio.file将java.io.File功能映射到java.nio.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 java.io.File API maps to in the java.nio.file API and tells you where you can obtain more information.这两个API之间没有一对一的对应关系,但下表让您大致了解java.io.File API中的哪些功能映射到java.nio.File API中,并告诉您可以从哪里获得更多信息。

java.io.File Functionality功能 java.nio.file Functionality功能 Tutorial Coverage教程覆盖
java.io.File java.nio.file.Path The Path Class路径类
java.io.RandomAccessFile The SeekableByteChannel functionality.SeekableByteChannel功能。 Random Access Files随机存取文件
File.canRead, canWrite, canExecute Files.isReadable, Files.isWritable, and Files.isExecutable.
On UNIX file systems, the Managing Metadata (File and File Store Attributes) package is used to check the nine file permissions.在UNIX文件系统上,管理元数据(文件和文件存储属性)包用于检查九个文件权限。
Checking a File or Directory检查文件或目录
Managing Metadata管理元数据
File.isDirectory(), File.isFile(), and File.length() Files.isDirectory(Path, LinkOption...), Files.isRegularFile(Path, LinkOption...), and Files.size(Path) Managing Metadata管理元数据
File.lastModified() and File.setLastModified(long) Files.getLastModifiedTime(Path, LinkOption...) and Files.setLastMOdifiedTime(Path, FileTime) Managing Metadata管理元数据
The File methods that set various attributes: setExecutable, setReadable, setReadOnly, setWritable These methods are replaced by the Files method setAttribute(Path, String, Object, LinkOption...).这些方法由Files方法setAttribute(Path, String, Object, LinkOption...)替换。 Managing Metadata管理元数据
new File(parent, "newfile") parent.resolve("newfile") Path Operations路径操作
File.renameTo Files.move Moving a File or Directory移动文件或目录
File.delete Files.delete Deleting a File or Directory删除文件或目录
File.createNewFile Files.createFile Creating Files创建文件
File.deleteOnExit Replaced by the DELETE_ON_CLOSE option specified in the createFile method.替换为createFile方法中指定的DELETE_ON_CLOSE选项。 Creating Files创建文件
File.createTempFile Files.createTempFile(Path, String, FileAttributes<?>), Files.createTempFile(Path, String, String, FileAttributes<?>) Creating Files创建文件
Creating and Writing a File by Using Stream I/O使用流I/O创建和写入文件
Reading and Writing Files by Using Channel I/O使用通道I/O读写文件
File.exists Files.exists and Files.notExists Verifying the Existence of a File or Directory验证文件或目录是否存在
File.compareTo and equals Path.compareTo and equals Comparing Two Paths比较两条路径
File.getAbsolutePath and getAbsoluteFile Path.toAbsolutePath Converting a Path转换路径
File.getCanonicalPath and getCanonicalFile Path.toRealPath or normalize Converting a Path转换路径 (toRealPath)
Removing Redundancies From a Path从路径中删除冗余 (normalize)
File.toURI Path.toURI Converting a Path转换路径
File.isHidden Files.isHidden Retrieving Information About the Path正在检索有关路径的信息
File.list and listFiles Path.newDirectoryStream Listing a Directory's Contents列出目录的内容
File.mkdir and mkdirs Files.createDirectory Creating a Directory创建目录
File.listRoots FileSystem.getRootDirectories Listing a File System's Root Directories列出文件系统的根目录
File.getTotalSpace, File.getFreeSpace, File.getUsableSpace FileStore.getTotalSpace, FileStore.getUnallocatedSpace, FileStore.getUsableSpace, FileStore.getTotalSpace File Store Attributes文件存储属性

Previous page: Other Useful Methods
Next page: Summary