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发行说明。
A few useful methods did not fit elsewhere in this lesson and are covered here. 一些有用的方法不适用于本课程的其他部分,本文将介绍这些方法。This section covers the following:本节包括以下内容:
To determine the MIME type of a file, you might find the 要确定文件的MIME类型,您可能会发现probeContentType(Path)
method useful. 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
方法。
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:*.*");
The path separator for POSIX file systems is the forward slash, POSIX文件系统的路径分隔符为正斜杠/
, and for Microsoft Windows is the backslash, \
. /
,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
方法还用于检索任何可用文件系统的路径分隔符。
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: 在Microsoft Windows中,每个卷都由文件存储区表示:C:
, D:
, and so on.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
方法。