public interface UserDefinedFileAttributeView extends FileAttributeView
这FileAttributeView
提供查看文件的自定义属性为一组名称/值对,属性名是由一个String
表示。一个实现可能需要进行编码和解码的平台或文件系统访问属性时表示。该值具有不透明的内容。此属性视图定义读取值或从一个ByteBuffer
写read
和write
方法。这FileAttributeView
不适合使用在一个属性值的大小是大于Integer.MAX_VALUE
。
用户定义的属性可以用在一些实现存储安全相关的属性,因此,至少在默认的提供程序的情况下,所有的方法,访问用户定义的属性要求RuntimePermission("accessUserDefinedAttributes")
许可时,安全管理器安装。
的supportsFileAttributeView
方法可以用来测试是否一个特定的FileStore
支持用户自定义属性存储。
在文件属性的动态访问是必需的,这getAttribute
方法可以用来读取属性值。属性值作为一个字节数组返回(字节)。的setAttribute
方法可以用于编写一个自定义属性从缓冲区的值(如调用write
法)、(字节或字节数组[ ])。
Modifier and Type | Method and Description |
---|---|
void |
delete(String name)
删除用户定义的属性。
|
List<String> |
list()
返回包含用户定义属性的名称的列表。
|
String |
name()
返回此属性视图的名称。
|
int |
read(String name, ByteBuffer dst)
将用户定义的属性的值转换为缓冲区。
|
int |
size(String name)
返回一个用户定义的属性的值的大小。
|
int |
write(String name, ByteBuffer src)
从缓冲区中写入用户定义属性的值。
|
String name()
"user"
观点。
name
接口
AttributeView
List<String> list() throws IOException
IOException
如果I/O错误发生
SecurityException
在默认提供程序的情况下,安装安全管理器,它否认
RuntimePermission
("accessUserDefinedAttributes")或其
checkRead
法否认对文件的读访问。
int size(String name) throws IOException
name
-属性名称
ArithmeticException
-如果属性尺寸大于
Integer.MAX_VALUE
IOException
如果I/O错误发生
SecurityException
在默认提供程序的情况下,安装安全管理器,它否认
RuntimePermission
("accessUserDefinedAttributes")或其
checkRead
法否认对文件的读访问。
int read(String name, ByteBuffer dst) throws IOException
此方法将属性的值读入给定的缓冲区中,作为一个字节序列,如果在缓冲区中剩余的字节数不足以读取完整的属性值,则失败了。转移到缓冲区中的字节数是n
,哪里n
是属性值的大小。序列中的第一个字节在指数p
和最后一个字节在指数p + n - 1
,哪里p
是缓冲区的位置。在返回的缓冲区的位置将等于p + n
;极限将不会改变。
使用示例:假设我们要读取文件的MIME类型,存储为一个用户定义的属性的名称的“user.mimetype
”。
userdefinedfileattributeview观=文件。getfileattributeview(路径,userdefinedfileattributeview。类);字符串“用户类型”;字节缓冲区buf = ByteBuffer。分配(观。大小(名字));观。读(姓名,buf);flip() buf中;字符串值=字符集。defaultcharset()。解码(BUF)。tostring();
name
-属性名称
dst
-目标缓冲区
IllegalArgumentException
-如果目标缓冲区是只读的
IOException
如果I/O错误发生或在属性值的目标缓冲区没有足够的空间
SecurityException
在默认提供程序的情况下,安装安全管理器,它否认
RuntimePermission
("accessUserDefinedAttributes")或其
checkRead
法否认对文件的读访问。
size(java.lang.String)
int write(String name, ByteBuffer src) throws IOException
此方法将属性的值从一个给定的缓冲区中写入一个字节序列。价值的大小是r
转移,在r
是剩余的缓冲区中的字节数,即src.remaining()
。字节序列从缓冲区开始在指数p
转移,在p
是缓冲区的位置。返回后,缓冲区的位置将等于p + n
,哪里n
是传输的字节数;限制将不会改变。
如果给定名称的属性已经存在,则它的值被替换。如果属性不存在,则创建它。如果它实现特定的,如果一个测试,以检查是否存在的属性和创建属性是原子相对于其他文件系统活动。
那里有足够的空间来存储属性,或属性的名称或值超过一个特定实现的最大尺寸然后IOException
抛出。
使用示例:假设我们要写一个文件的MIME类型为自定义属性:
userdefinedfileattributeview观=文件。getfileattributeview(路径,userdefinedfileattributeview。类);看来,写(“用户类型”,字符集。defaultcharset()。编码(“text/html”));
name
-属性名称
src
包含属性值的缓冲区
IOException
如果I/O错误发生
SecurityException
在默认提供程序的情况下,安装安全管理器,它否认
RuntimePermission
("accessUserDefinedAttributes")或其
checkWrite
方法拒绝写访问文件。
void delete(String name) throws IOException
name
-属性名称
IOException
-如果发生I/O错误或不存在的属性
SecurityException
在默认提供程序的情况下,安装安全管理器,它否认
RuntimePermission
("accessUserDefinedAttributes")或其
checkWrite
方法拒绝写访问文件。
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.