Documentation

The Java™ Tutorials
Hide TOC
Other Useful Methods其他有用的方法
Trail: Essential Java Classes
Lesson: Basic I/O
Section: File I/O (Featuring NIO.2)

Other Useful Methods其他有用的方法

A few useful methods did not fit elsewhere in this lesson and are covered here. 一些有用的方法不适用于本课程的其他部分,本文将介绍这些方法。This section covers the following:本节包括以下内容:

Determining MIME Type确定MIME类型

To determine the MIME type of a file, you might find the probeContentType(Path) method useful. 要确定文件的MIME类型,您可能会发现probeContentType(Path)方法很有用。For example:例如:

try {
    String type = Files.probeContentType(filename);
    if (type == null) {
        System.err.format("'%s' has an" + " unknown filetype.%n", filename);
    } else if (!type.equals("text/plain") {
        System.err.format("'%s' is not" + " a plain text file.%n", filename);
        continue;
    }
} catch (IOException x) {
    System.err.println(x);
}

Note that probeContentType returns null if the content type cannot be determined.请注意,如果无法确定内容类型,probeContentType将返回null

The implementation of this method is highly platform specific and is not infallible. 此方法的实现是高度平台特定的,并非绝对正确。The content type is determined by the platform's default file type detector. 内容类型由平台的默认文件类型检测器确定。For example, if the detector determines a file's content type to be application/x-java based on the .class extension, it might be fooled.例如,如果检测器根据.class扩展名确定文件的内容类型为application/x-java,那么它可能会被愚弄。

You can provide a custom FileTypeDetector if the default is not sufficient for your needs.如果默认值不足以满足您的需要,则可以提供自定义FileTypeDetector

The Email example uses the probeContentType method.Email示例使用probeContentType方法。

Default File System默认文件系统

To retrieve the default file system, use the getDefault method. 要检索默认文件系统,请使用getDefault方法。Typically, this FileSystems method (note the plural) is chained to one of the FileSystem methods (note the singular), as follows:通常,此FileSystems方法(注意复数)连缀到其中一个FileSystem方法(注意单数),如下所示:

PathMatcher matcher =
    FileSystems.getDefault().getPathMatcher("glob:*.*");

Path String Separator路径字符串分隔符

The path separator for POSIX file systems is the forward slash, /, and for Microsoft Windows is the backslash, \. POSIX文件系统的路径分隔符为正斜杠/,Microsoft Windows的路径分隔符为反斜杠\Other file systems might use other delimiters. 其他文件系统可能使用其他分隔符。To retrieve the Path separator for the default file system, you can use one of the following approaches:要检索默认文件系统的Path分隔符,可以使用以下方法之一:

String separator = File.separator;
String separator = FileSystems.getDefault().getSeparator();

The getSeparator method is also used to retrieve the path separator for any available file system.getSeparator方法还用于检索任何可用文件系统的路径分隔符。

File System's File Stores文件系统的文件存储

A file system has one or more file stores to hold its files and directories. 文件系统有一个或多个文件存储来保存其文件和目录。The file store represents the underlying storage device. 文件存储区表示底层存储设备。In UNIX operating systems, each mounted file system is represented by a file store. 在UNIX操作系统中,每个装入的文件系统都由一个文件存储表示。In Microsoft Windows, each volume is represented by a file store: C:, D:, and so on.在Microsoft Windows中,每个卷都由文件存储区表示:C:D:,等等。

To retrieve a list of all the file stores for the file system, you can use the getFileStores method. 要检索文件系统中所有文件存储的列表,可以使用getFileStores方法。This method returns an Iterable, which allows you to use the enhanced for statement to iterate over all the root directories.此方法返回一个Iterable,它允许您使用增强的for语句迭代所有根目录。

for (FileStore store: FileSystems.getDefault().getFileStores()) {
   ...
}

If you want to retrieve the file store where a particular file is located, use the getFileStore method in the Files class, as follows:如果要检索特定文件所在的文件存储,请在Files类中使用getFileStore方法,如下所示:

Path file = ...;
FileStore store= Files.getFileStore(file);

The DiskUsage example uses the getFileStores method.DiskUsage示例使用getFileStores方法。


Previous page: Watching a Directory for Changes
Next page: Legacy File I/O Code