`
cucaracha
  • 浏览: 137977 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
博客专栏
A8f3fa2f-18ce-3862-897a-5d2209197c90
Java NIO.2
浏览量:85973
7a076ba7-8ec8-3241-aa3c-67bb2f7856a2
Java EE 7 简明教...
浏览量:35733
社区版块
存档分类
最新评论

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

阅读更多
大多数文件系统都有文件所有者的概念,并且决定了访问文件系统对象的权限。在 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 支持以下属性名称:
  • owner

访问属性的通用结构是 [view-name:]attribute-name,在这个例子中 view-name 是 owner,attribute-name 是 owner。

文章来源:http://www.aptusource.org/2014/03/nio-2-file-owner-view/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics