public interface UserDefinedFileAttributeView extends FileAttributeView
此FileAttributeView提供了一个文件的用户定义属性作为一组名称/值对的视图,其中属性名称由String 。 实现可能需要在访问属性时从平台或文件系统表示进行编码和解码。 该值具有不透明的内容。 此属性视图定义read种write方法来读出的值中或从一个写ByteBuffer 。 该FileAttributeView不适用于属性值的大小大于Integer.MAX_VALUE的位置 。
用户定义的属性可以在一些实现被用来存储与安全相关的属性,这样因此,在默认提供的情况下,至少,访问用户自定义属性的所有方法都需要RuntimePermission("accessUserDefinedAttributes")安装了安全管理器时许可。
supportsFileAttributeView方法可用于测试特定的FileStore是否支持用户定义属性的存储。
在需要动态访问文件属性的情况下,可以使用getAttribute方法来读取属性值。 属性值作为字节数组返回(byte [])。 setAttribute方法可用于从缓冲区(如通过调用write方法)或字节数组(byte [])写入用户定义属性的值。
| 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 view =
Files.getFileAttributeView(path, UserDefinedFileAttributeView.class);
String name = "user.mimetype";
ByteBuffer buf = ByteBuffer.allocate(view.size(name));
view.read(name, buf);
buf.flip();
String value = Charset.defaultCharset().decode(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()的字节数,即src.remaining() 。 从索引p开始的缓冲区中传输字节序列,其中p是缓冲区的位置。 返回时,缓冲区的位置将等于p + n ,其中n是n的字节数; 其限制将不会改变。
如果给定名称的属性已经存在,那么它的值将被替换。 如果属性不存在,则创建它。 如果实现具体,如果检查属性的存在和属性的创建的测试对于其他文件系统活动是原子的。
如果没有足够的空间来存储属性,或者属性名称或值超过实现特定的最大大小,则抛出IOException 。
使用示例:假设我们要将文件的MIME类型写成用户定义的属性:
UserDefinedFileAttributeView view =
FIles.getFileAttributeView(path, UserDefinedFileAttributeView.class);
view.write("user.mimetype", Charset.defaultCharset().encode("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.