如果你发现 NIO.2 内置的属性视图不能满足你的要求,或者你想给文件设置某些特殊的属性,那么你可以使用自定义属性视图。NIO.2 提供了 UserDefinedFileAttributeView 接口来支持这个功能。使用这个视图,你可以添加任何你认为有用的文件属性。
检查是否支持自定义文件属性
在你要创建自己的文件属性之前,要先检验文件系统是否支持这个功能。可以调用 FileStore.supportsFileAttributeView() 方法来进行检验,这个方法接受一个 String 类型的参数,可以传入视图名称进行检验,也接受 Class 类型的参数,可传入视图的 Class 进行检验。下面看看样例:
import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.UserDefinedFileAttributeView;
…
Path path = Paths.get("C:/rafaelnadal/tournaments/2009", "BNP.txt");
try {
FileStore store = Files.getFileStore(path);
if (!store.supportsFileAttributeView(UserDefinedFileAttributeView.class)) {
System.out.println("The user defined attributes are not supported on: " + store);
} else {
System.out.println("The user defined attributes are supported on: " + store);
}
} catch (IOException e) {
System.err.println(e);
}
当然,你也可以通过 FileSystem 来获取 FileStore,这样可以检验所有的 FileStore 是否支持某个属性视图。
自定义属性的操作
如果你的文件系统支持自定义属性。那么就可以进行添加属性、查询属性、删除属性、更新属性的操作。
定义属性
首先我们定义一个名为 file.description 值为 This file contains
private information! 的属性。当你调用 Files.getFileAttributeView() 方法得到文件视图后,可以通过下面的方法写出属性:
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.UserDefinedFileAttributeView;
…
Path path = Paths.get("C:/rafaelnadal/tournaments/2009", "BNP.txt");
UserDefinedFileAttributeView udfav = Files.getFileAttributeView(path,
UserDefinedFileAttributeView.class);
try {
int written = udfav.write("file.description", Charset.defaultCharset().
encode("This file contains private information!"));
} catch (IOException e) {
System.err.println(e);
}
可以看到 write() 方法接受两个参数,第一个参数表示属性名,第二个参数是 ByteBuffer 类型,表示属性值。如果属性名已经存在,则会被替换。这个方法还返回一个 int 类型的返回值,表示写出的字节数,可以为 0。
也可以通过 Files.setAttribute() 来设置属性,属性值可用 ByteBuffer 类型或 byte 数组类型(byte[])。
获取属性名称和值的大小
在任何时候,你都可以调用 UserDefinedFileAttributeView.list() 方法来列出自定义属性的名称。这个方法返回 List 类型的返回值,List 中用 String 类型存储自定义的属性名。将属性名传入 UserDefinedFileAttributeView.size() 方法可得到属性值的大小,下面看例子:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.UserDefinedFileAttributeView;
…
Path path = Paths.get("C:/rafaelnadal/tournaments/2009", "BNP.txt");
UserDefinedFileAttributeView udfav = Files.getFileAttributeView(path,
UserDefinedFileAttributeView.class);
try {
for (String name : udfav.list()) {
System.out.println(udfav.size(name) + " " + name);
}
} catch (IOException e) {
System.err.println(e);
}
获取自定义属性值
可以通过 UserDefinedFileAttributeView.read() 方法来读取自定义属性值,这个方法接受两个参数,类型分别为 String 和 ByteBuffer,Sting 参数传入属性名,ByteBuffer 参数用来存放属性值。下面看看例子:
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.UserDefinedFileAttributeView;
…
Path path = Paths.get("C:/rafaelnadal/tournaments/2009", "BNP.txt");
UserDefinedFileAttributeView udfav = Files.getFileAttributeView(path,
UserDefinedFileAttributeView.class);
try {
int size = udfav.size("file.description");
ByteBuffer bb = ByteBuffer.allocateDirect(size);
udfav.read("file.description", bb);
bb.flip();
System.out.println(Charset.defaultCharset().decode(bb).toString());
} catch (IOException e) {
System.err.println(e);
}
从上面的例子可以看到,使用 UserDefinedFileAttributeView.size() 方法,可以很容易地设置存放属性值的 ByteBuffer 的长度。
另外,也可以通过 Files.getAttribute() 来获取属性值,这会返回 byte 类型的数组(byte[])。
删除自定义属性
当用户自定义的属性不再有用的时候,可以调用 UserDefinedFileAttributeView.delete() 方法来删除属性值。只需传入属性名称即可,下面看看例子:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.UserDefinedFileAttributeView;
…
Path path = Paths.get("C:/rafaelnadal/tournaments/2009", "BNP.txt");
UserDefinedFileAttributeView udfav = Files.getFileAttributeView(path,
UserDefinedFileAttributeView.class);
try {
udfav.delete("file.description");
} catch (IOException e) {
System.err.println(e);
}
文章来源:
http://www.aptusource.org/2014/03/nio-2-user-defined-file-attribute-view/
分享到:
相关推荐
Java I/O, NIO, and NIO.2 is a power-packed book that accelerates your mastery of Java's various I/O APIs. In this book, you'll learn about classic I/O APIs (File, RandomAccessFile, the stream classes ...
java NIO.zip
New I/O (NIO), and NIO.2 categories. You learn what each category offers in terms of its capabilities, and you also learn about concepts such as paths and Direct Memory Access. Chapters 2 through 5 ...
这是一本介绍java io以及nio相关知识的书,书中对知识的讲解通俗易懂,是学习java nio以及复习java io相关知识的必备书籍。注意:本书为英文版!!!
java nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava ...
Java NIO.pdf Java NIO.pdf Java NIO.pdf Java NIO.pdf Java NIO.pdf Java NIO.pdf
蔚来-NIO.US-新车上市,蔚来可期.pdf
This book covers all the important aspects involved in developing NIO.2-based applications. It provides clear instructions for getting the most out of NIO.2 and offers many exercises and case studies ...
Apress.Pro.Java.7.NIO.2.2011
Java NIO,Ron Hitchens 著,中文版 裴小星 译,Pro Java 7 NIO.2,Anghel Leonard 著,pdf文字版带书签,无安全限制
Pro Java 7 NIO.2 – PDF Books
Pro Java 7 NIO.2.pdf,2011 by Anghel Leonard
This book covers all the important aspects involved in developing NIO.2-based applications. It provides clear instructions for getting the most out of NIO.2 and offers many exercises and case studies ...
JavaNIO.pdf
定义作为数据容器的缓冲区,并提供其他 NIO 包的概述。 NIO API 的集中抽象为: 缓冲区,它们是数据容器; 字符集 及其相关解码器 和编码器, 它们在字节和 Unicode 字符之间进行转换; 各种类型的通道,...
找了好久,终于找到了,java刷新同步获取网络资源
用java.nio.*进行网络编程
Java NIO.docx