0

在 Linux 主机上的 Java 中,我需要能够将任意文件的 POSIX 组设置为由传入的 gid 指定的组。

我知道要修改 Java 中的组,我可以这样做:

UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName(groupName);
Files.getFileAttributeView(myFile.toPath(), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).setGroup(group);

但是,这是基于知道组名,而我只知道 gid。无论如何要在Java中获取给定组ID的组名,还是在Java中直接设置给定gid的文件组?由于保证运行该进程的用户是指定 gid 的成员,因此如果我可以获得用户所属的每个 gid -> groupName 对的 Map,那将起作用。

4

0 回答 0