现在的位置: 首页 > 综合 > 正文

管理元数据(Managing Metadata)

2013年05月16日 ⁄ 综合 ⁄ 共 3070字 ⁄ 字号 评论关闭

Java SE 7.0在java.nio.File包中加入了很多新的类和接口用于更方便的操控文件和目录,本文在学习新的API的过程中对于新加入的比较有意义的元数据管理部分进行了描述与说明,大多内容来自于Oracle官方的java教程(Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)

文件的元数据又称为"关于数据的数据",就文件系统而言,元数据包含于文件或目录中,元数据用于追踪下面这些对象中每一个的信息,例如:是否为普通文件、目录或符号链接,文件的大小,文件或目录的创建时间、修改时间以及最后访问时间,文件的拥有者、组拥有者以及文件的访问许可等。文件系统的元数据通常又被称为文件属性。文件类(Files)提供了一些方法来获得或设置文件的单一属性。其方法列表如下:

             

Methods

Comment

size(Path)

获得文件的大小(以字节为单位)

isDirectory(Path, LinkOption)

判断是否为目录

isRegularFile(Path, LinkOption...)

判断是否为普通文件

isSymbolicLink(Path)

判断是否为符号链接

isHidden(Path)

判断是否为隐藏属性

getLastModifiedTime(Path, LinkOption...)
setLastModifiedTime(Path, FileTime)

得到或设置文件的最后修改时间

getOwner(Path, LinkOption...)
setOwner(Path, UserPrincipal)

得到或设置文件的拥有者

getPosixFilePermissions(Path, LinkOption...)
setPosixFilePermissions(Path, Set<PosixFilePermission>)

得到或设置Posix文件的的许可

getAttribute(Path, String, LinkOption...)
setAttribute(Path, String, Object, LinkOption...)

以字符串作为参数得到相应的文件属性或设置文件属性,Object

表示属性值类型(其字符串表示的属性及相应的属性值见下表)

               

基于文件属性是与文件系统相关的,在java SE 7.0中通过几个基本的接口来描述相应的文件系统属性以及文件的拥有者,其接口为: BasicFileAttributesDosFileAttributes、PosixFileAttributes、UserPrincipal、GroupPrincipal,最后还有二个实现类AclEntry、FileStoreAclEntry用于支持ACL模型的文件系统。下面就这些接口中描述的属性以及属性值列表如下:

属性

属性值

"lastModifiedTime"

FileTime

"lastAccessTime"

FileTime

"creationTime"

FileTime

"size"

Long

"isRegularFile"

Boolean

"isDirectory"

Boolean

"isSymbolicLink"

Boolean

"isOther"

Boolean

"fileKey"

Object

"readonly"

Boolean

"hidden"

Boolean

"system"

Boolean

"archive"

Boolean

"permissions"

Set<PosixFilePermission>

"group"

GroupPrincipal

"owner"

UserPrincipal

"acl"

List<AclEntry>

               

同时提供了泛型接口FileAttribute<T>来封装属性值对,用于在创建新的文件或目录时候通过调用 createFile  createDirectory方法来设置文件或目录的属性。UserDefinedFileAttributeView中定义了相关的write和read方法来写入和读取用户定义的属性值对。

上述方法适用于单一属性,有时同时需要访问同一文件多个属性,使用上述方法来逐一获取属性是低效的且反复的访问文件系统来获取属性也会对系统的运行产生有害的影响。基于该原因,文件类提供了块操作方法来读取属性,其块操作方法列表如下:

Methods

Comment

readAttributes(Path, String, LinkOption...)

读取文件的属性,字符串参数表明了要读取的属性的内容

readAttributes(Path, Class<A>, LinkOption...)

读取文件属性,Class<A>代表了要读取的文件属性表示的类型

               

块操作读取属性也是基于相应的文件系统的,因此把相关的文件属性被编组成视图(view),每个视图被映射到一个具体的文件系统的实现或一个共有的功能例如文件拥有者,java SE 7.0中加入的视图如下:

  • BasicFileAttributeView – Provides a view of basic attributes that are required to be supported by all file system implementations.
  • DosFileAttributeView – Extends the basic attribute view with the standard four bits supported on file systems that support the DOS attributes.
  • PosixFileAttributeView – Extends the basic attribute view with attributes supported on file systems that support the POSIX family of standards, such as UNIX. These attributes include file owner, group owner, and the nine related access permissions.
  • FileOwnerAttributeView – Supported by any file system implementation that supports the concept of a file owner.
  • AclFileAttributeView – Supports reading or updating a file's Access Control Lists (ACL). The NFSv4 ACL model is supported. Any ACL model, such as the Windows ACL model, that has a well-defined mapping to the NFSv4 model might also be supported.
  • UserDefinedFileAttributeView – Enables support of metadata that is user defined. This view can be mapped to any extension mechanisms that a system supports. In the Solaris OS, for example, you can use this view to store the MIME type of a file.

一个具体的文件系统可能只支持基本的文件视图,也可能同时支持以上视图中的几个,也有可能支持7.0引入的API以外的其它视图。

抱歉!评论已关闭.