[NIO.2] 第十四篇 属性视图之 File Owner View

大多数文件系统都有文件所有者的概念,并且决定了访问文件系统对象的权限。在 NIO.2 中,提供了 UserPrincipal 接口来关联文件所有者这个概念,并且提供了 FileOwenerAttributeView 接口来设置和读取文件所有者。

注:在本文的例子中使用的文件所有者名称为“apress”,但是在你的系统上可能没有这个用户。运行测试代码的时候会抛出 java.nio.file.attribute.UserPrincipalNotFoundException,你需要添加这个用户在你的机器上才能正常运行(这个用户必须拥有管理员权限或其它适合的系统权限)。

[b][size=x-large]使用 Files.setOwner() 设置文件所有者[/size][/b]

可以调用 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);
}


[b][size=x-large]使用 FileOwnerAttributeView.setOwner() 设置文件所有者[/size][/b]

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);
}


[b][size=x-large]使用 Files.setAttribute() 设置文件所有者[/size][/b]

和大多数属性视图一样,文件所有者属性也可以通过通用的 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);
}


[b][size=x-large]使用 FileOwnerAttributeView.getOwner() 获取文件所有者[/size][/b]

在判断文件使用权限的时候,常常需要获取文件所有者。 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);
}


[b][size=x-large]使用 Files.getAttribute() 获取文件所有者[/size][/b]

最后一个例子,我们使用通用的 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 支持以下属性名称:
[list]
[*]owner
[/list]
访问属性的通用结构是 [view-name:]attribute-name,在这个例子中 view-name 是 owner,attribute-name 是 owner。

文章来源:[url]http://www.aptusource.org/2014/03/nio-2-file-owner-view/[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值