对于 Unix 用户来说是个好消息!POSIX 视图扩展 Basic 视图并支持 Unix 及其它相关操作系统。POSIX 视图支持文件所有者、组拥有者、以及九个访问权限。
基于 PosixFileAttributes 类,可以通过下面的方式得到 POSIX 属性:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFileAttributes;
…
PosixFileAttributes attr = null;
Path path = Paths.get("/home/rafaelnadal/tournaments/2009/BNP.txt");
try {
attr = Files.readAttributes(path, PosixFileAttributes.class);
} catch (IOException e) {
System.err.println(e);
}
System.out.println("File owner: " + attr.owner().getName());
System.out.println("File group: " + attr.group().getName());
System.out.println("File permissions: " + attr.permissions().toString());
或者,也可以通过 Files.getFileAttributeView() 来获取 POSIX 属性:
import java.nio.file.attribute.PosixFileAttributeView;
…
try {
attr = Files.getFileAttributeView(path,
PosixFileAttributeView.class).readAttributes();
} catch (IOException e) {
System.err.println(e);
}
Posix 属性视图支持以下属性名:
访问属性的通用结构是 [view-name:]attribute-name,在这里 view-name 是 posix。
POSIX 文件权限
调用 permissions() 方法会返回 PosixFilePermission 对象列表。PosixFilePermissions 是一个文件权限的帮助类,它有一个比较有用的方法是 asFileAttribute(),这个方法接受一个 Set 类型的权限列表,然后可以将返回值传给 Path.createFile() 方法或者 Path.createDirectory() 方法。这可以用于创建一个新文件,并让新文件和已有文件具有相同权限的时候:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;
…
Path new_path = Paths.get("/home/rafaelnadal/tournaments/2009/new_BNP.txt");
FileAttribute<Set> posixattrs =
PosixFilePermissions.asFileAttribute(attr.permissions());
try {
Files.createFile(new_path, posixattrs);
} catch (IOException e) {
System.err.println(e);
}
Moreover, you can set a file’s permissions as a hard-coded string by calling the fromString()
method:
Set permissions = PosixFilePermissions.fromString("rw-r--r--");
try {
Files.setPosixFilePermissions(new_path, permissions);
} catch (IOException e) {
System.err.println(e);
}
POSIX 组拥有者
Posix 属性视图中,组拥有者属性名称是 group。setGroup() 方法可以传入一个 GroupPrincipal 类型的参数来设置组拥有者,GroupPrincipal 继承自 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.GroupPrincipal;
import java.nio.file.attribute.PosixFileAttributeView;
…
Path path = Paths.get("/home/rafaelnadal/tournaments/2009/BNP.txt");
try {
GroupPrincipal group = path.getFileSystem().
getUserPrincipalLookupService().lookupPrincipalByGroupName("apressteam");
Files.getFileAttributeView(path, PosixFileAttributeView.class).setGroup(group);
} catch (IOException e) {
System.err.println(e);
}
注意:在上面的例子中使用了名为“apressteam”的组,这在你的机器上可能不存在,因此你在运行上面的代码时会抛出 java.nio.file.attribute.UserPrincipalNotFoundException 异常,要想正常运行,你需要在你的机器上添加正确的组名(管理员组或其它适合权限的组)。
可以直接调用 Files.getAttribute() 来得到组拥有者:
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
…
try {
GroupPrincipal group = (GroupPrincipal) Files.getAttribute(path, "posix:group",
NOFOLLOW_LINKS);
System.out.println(group.getName());
} catch (IOException e) {
System.err.println(e);
}
注:因为 PosixFileAttributeView 继承了 FileOwnerAttributeView 接口,因此可以直接调用 getOwner() 和 setOwner() 方法来操作文件所有者。
文章来源:
http://www.aptusource.org/2014/03/nio-2-posix-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