15.10.4 访问文件属性
15.10.4 访问文件属性
早期的Java提供的File类可以访问一些简单的文件属性,比如文件大小、修改时间、文件是否隐藏、是文件还是目录等。如果程序需要获取或修改更多的文件属性,则必须利用运行所在平台的特定代码来实现,这是一件非常困难的事情。
文件属性工具类
Java7的NIO.2在java.nio.file.attribute包下提供了大量的工具类,通过这些工具类,开发者可以非常简单地读取、修改文件属性。这些工具类主要分为如下两类
| 类 | 描述 |
|---|---|
XxxAttributeView |
代表某种文件属性的“视图” |
XxxAttributes |
代表某种文件属性的“集合”,程序一般通过XxxAttributeView对象来获取XxxAttributes。 |
FileAttributeView
在这些工具类中,FileAttributeView是其他XxxAttributeView的父接口,下面简单介绍一下这些XxxAttributeView。
AclFileAttributeView
通过AclFileAttributeView,开发者可以为特定文件设置ACL(Access Control List)及文件所有者属性。它的getAcl()方法返回List<AclEntry>对象,该返回值代表了该文件的权限集。通过setAcl(List)方法可以修改该文件的ACL
| 方法 | 描述 |
|---|---|
List<AclEntry> getAcl() |
Reads the access control list. |
String name() |
Returns the name of the attribute view. |
void setAcl(List<AclEntry> acl) |
Updates (replace) the access control list. |
BasicFileAttributeView
它可以获取或修改文件的基本属性,包括文件的最后修改时间、最后访问时间、创建时间、大小、是否为目录、是否为符号链接等。它的readAttributes()方法返回一个BasicFileAttributes对象,对文件夹基本属性的修改是通过BasicFileAttributes对象完成的。
| BasicFileAttributeView的readAttributes方法 | 描述 |
|---|---|
BasicFileAttributes readAttributes() |
Reads the basic file attributes as a bulk operation. |
BasicFileAttributes
| BasicFileAttributes接口方法 | 描述 |
|---|---|
FileTime creationTime() |
Returns the creation time. |
Object fileKey() |
Returns an object that uniquely identifies the given file, or null if a file key is not available. |
boolean isDirectory() |
Tells whether the file is a directory. |
boolean isOther() |
Tells whether the file is something other than a regular file, directory, or symbolic link. |
boolean isRegularFile() |
Tells whether the file is a regular file with opaque content. |
boolean isSymbolicLink() |
Tells whether the file is a symbolic link. |
FileTime lastAccessTime() |
Returns the time of last access. |
FileTime lastModifiedTime() |
Returns the time of last modification. |
long size() |
Returns the size of the file (in bytes). |
DosFileAttributeView
它主要用于获取或修改文件**DOS相关属性**,比如文件是否只读、是否隐藏、是否为系统文件、是否是存档文件等。它的readAttributes方法返回一个DosFileAttributes对象,对这些属性的修改其实是由DosFileAttributes对象来完成的|
DosFileAttributes
DosFileAttributes接口方法 |
描述 |
|---|---|
boolean isArchive() |
Returns the value of the archive attribute. |
boolean isHidden() |
Returns the value of the hidden attribute. |
boolean isReadOnly() |
Returns the value of the read-only attribute. |
boolean isSystem() |
Returns the value of the system attribute. |
FileOwnerAttributeView
它主要用于获取或修改文件的所有者。它的getOwner()方法返回一个UserPrincipal对象来代表文件所有者;也可调用setOwner(UserPrincipal owner)方法来改变文件的所有者
FileOwnerAttributeView方法 |
描述 |
|---|---|
UserPrincipal getOwner() |
Read the file owner. |
void setOwner(UserPrincipal owner) |
Updates the file owner. |
PosixFileAttributeView
它主要用于获取或修改POSX(Portable Operating System Interface of INIX)属性,它的readAttributes()方法返回一个PosixFileAttributes对象,该对象可用于获取或修改文件的所有者、组所有者、访问权限信息(就是UNIX的chmod命令负责干的事情)。这个view只在UNNX、Linux等系统上有用。
| PosixFileAttributeView方法 | 描述 |
|---|---|
PosixFileAttributes readAttributes() |
Reads the basic file attributes as a bulk operation. |
PosixFileAttributes
| PosixFileAttributes接口方法 | 描述 |
|---|---|
GroupPrincipal group() |
Returns the group owner of the file. |
UserPrincipal owner() |
Returns the owner of the file. |
Set<PosixFilePermission> permissions() |
Returns the permissions of the file. |
UserDefinedFileAttributeView
它可以让开发者为文件设置一些自定义属性。
程序 读取 修改文件属性
下面程序示范了如何读取、修改文件的属性。
1 | import java.nio.ByteBuffer; |
上面程序中的分别访问了4种不同类型的文件属性,关于读取、修改文件属性的说明,程序中的代码已有详细说明,因此不再过多地解释。第二次运行该程序(记住第一次运行后AttributeViewTestJava文件变成隐藏、只读文件,因此第二次运行之前一定要先取消只读属性)