linux Java创建具有特定所有者(用户/组)的文件和目录 1 月,3 周 Questions & Answers 11172 在Java中,是否可以使用不同的用户/组管理创建文件/目录(如果程序以ROOT用户身份运行)
# 1 楼答案 可以在JDK 7中使用新的IO API(Java NIO)实现这一点 有getOwner()/setOwner()方法可用于管理文件的所有权,也可以使用PosixFileAttributeView.setGroup()来管理组 下面的代码片段显示了如何使用setOwner方法设置文件所有者: Path file = ...; UserPrincipal owner = file.GetFileSystem().getUserPrincipalLookupService() .lookupPrincipalByName("sally"); Files.setOwner(file, owner); Files类中没有用于设置组所有者的专用方法。但是,直接执行此操作的安全方法是通过POSIX文件属性视图,如下所示: Path file = ...; GroupPrincipal group = file.getFileSystem().getUserPrincipalLookupService() .lookupPrincipalByGroupName("green"); Files.getFileAttributeView(file, PosixFileAttributeView.class) .setGroup(group);
# 1 楼答案
可以在JDK 7中使用新的IO API(Java NIO)实现这一点
有getOwner()/setOwner()方法可用于管理文件的所有权,也可以使用PosixFileAttributeView.setGroup()来管理组
下面的代码片段显示了如何使用setOwner方法设置文件所有者:
Files类中没有用于设置组所有者的专用方法。但是,直接执行此操作的安全方法是通过POSIX文件属性视图,如下所示: