大多数文件系统都有文件所有者的概念,并且决定了访问文件系统对象的权限。在 NIO.2 中,提供了 UserPrincipal 接口来关联文件所有者这个概念,并且提供了 FileOwenerAttributeView 接口来设置和读取文件所有者。
注:在本文的例子中使用的文件所有者名称为“apress”,但是在你的系统上可能没有这个用户。运行测试代码的时候会抛出 java.nio.file.attribute.UserPrincipalNotFoundException,你需要添加这个用户在你的机器上才能正常运行(这个用户必须拥有管理员权限或其它适合的系统权限)。
使用 Files.setOwner() 设置文件所有者
可以调用 Files.setOwner() 来设置文件所有者。这个方法需要传入 UserPrincipal 类型的对象。可以通过调用 FileSystem.getUserPrincipalLookupService() 方法来获取用户查找服务,然后调用 lookupPrincipalByName() 方法得到 UserPrincipal 对象,下面看看例子:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.UserPrincipal;
...
UserPrincipal owner = null;
Path path = Paths.get("C:/rafaelnadal/tournaments/2009", "BNP.txt");
try {
owner = path.getFileSystem().getUserPrincipalLookupService().
lookupPrincipalByName("apress");
Files.setOwner(path, owner);
} catch (IOException e) {
System.err.println(e);
}
使用 FileOwnerAttributeView.setOwner() 设置文件所有者
FileOwnerAttributeView 属性视图支持读取和设置文件所有者。这个属性视图的名称为 owner,设置文件所有者的时候也需要 UserPrincipal 类型的对象,下面看看例子:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileOwnerAttributeView;
import java.nio.file.attribute.UserPrincipal;
...
UserPrincipal owner = null;
Path path = Paths.get("C:/rafaelnadal/tournaments/2009", "BNP.txt");
FileOwnerAttributeView foav = Files.getFileAttributeView(path,
FileOwnerAttributeView.class);
try {
owner = path.getFileSystem().getUserPrincipalLookupService().
lookupPrincipalByName("apress");
foav.setOwner(owner);
} catch (IOException e) {
System.err.println(e);
}
使用 Files.setAttribute() 设置文件所有者
和大多数属性视图一样,文件所有者属性也可以通过通用的 setAttribute() 方法来进行设置,这个属性的完整名称是 owner:owner,下面看看例子:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.UserPrincipal;
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
…
UserPrincipal owner = null;
Path path = Paths.get("C:/rafaelnadal/tournaments/2009", "BNP.txt");
try {
owner = path.getFileSystem().getUserPrincipalLookupService().
lookupPrincipalByName("apress");
Files.setAttribute(path, "owner:owner", owner, NOFOLLOW_LINKS);
} catch (IOException e) {
System.err.println(e);
}
使用 FileOwnerAttributeView.getOwner() 获取文件所有者
在判断文件使用权限的时候,常常需要获取文件所有者。 getOwner() 方法返回文件所有者的 UserPrincipal 对象。可以调用 UserPrincipal.getName() 方法将对象转换为 String 类型。
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileOwnerAttributeView;
…
Path path = Paths.get("C:/rafaelnadal/tournaments/2009", "BNP.txt");
FileOwnerAttributeView foav = Files.getFileAttributeView(path,
FileOwnerAttributeView.class);
try {
String owner = foav.getOwner().getName();
System.out.println(owner);
} catch (IOException e) {
System.err.println(e);
}
使用 Files.getAttribute() 获取文件所有者
最后一个例子,我们使用通用的 Files.getAttribute() 来获取文件所有者。
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.UserPrincipal;
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
…
Path path = Paths.get("C:/rafaelnadal/tournaments/2009", "BNP.txt");
try {
UserPrincipal owner = (UserPrincipal) Files.getAttribute(path,
"owner:owner", NOFOLLOW_LINKS);
System.out.println(owner.getName());
} catch (IOException e) {
System.err.println(e);
}
注意:在使用用户查找服务(principal lookup service)的时候,如果用户不存在或者传入了错误的用户名,将会抛出 java.nio.file.attribute.UserPrincipalNotFoundException 异常。
File Owner 支持以下属性名称:
访问属性的通用结构是 [view-name:]attribute-name,在这个例子中 view-name 是 owner,attribute-name 是 owner。
文章来源:
http://www.aptusource.org/2014/03/nio-2-file-owner-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
Chapters 12 through 14 cover NIO.2’s improved file system interface, asynchronous I/O, and the completion of socket channel functionality. Each chapter ends with assorted exercises that are designed...
这是一本介绍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
但 Java 在后期版本中引入了 java.nio.file 库来提高 Java 对文件操作的能力。还增加的流的功能,似乎使得文件变成更好用了。所以本章,我们就来主要介绍 java.nio.file 中常用的类和模块,大致如下: Path 路径:...
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
下面小编就为大家分享一篇JDK1.7 之java.nio.file.Files 读取文件仅需一行代码实现,具有很好的参考价值,希望对大家有所帮助
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
nio.file java.nio.file 的 Clojure 包装器为什么要这样做? 看看 。 工作正在进行中! 在我们达到 1.0.0 之前,预计会有错误和缺失的功能。支持的 Clojure 和 Java 版本nio.file针对 Clojure nio.file和 Java 1.7+...
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 ...
java.io clojure.java.io 的 JK7 java.nio.file.Path 兼容性依赖信息该库托管在 Releases 上。 依赖: [me.moocar/java.io " 0.1.0 " ]用法是 JDK7 中引入的文件路径的抽象。 这个库提供了和 Paths 之间的兼容性。 ...
JavaNIO.pdf
找了好久,终于找到了,java刷新同步获取网络资源