我如何在java中进行lcown,它可以在所有Unix版本中工作:Linux、redhat、hp-ux、rhel-5和S



我有filepath、userId和groupId,我如何在java中进行lcown(用于符号链接),它将在所有unix风格中工作:Linux、redhat、hp-ux、rhel-5和Solaris

我试着探索java.nio.file包,但找不到能帮助我实现这一点的函数。

这相当于戈朗:os。Lchown(filePath,uid,gid)——如何在java中实现这一点?

您应该看看这些:

https://docs.oracle.com/javase/8/docs/api/java/nio/file/attribute/FileOwnerAttributeView.html

https://docs.oracle.com/javase/8/docs/api/java/nio/file/attribute/PosixFilePermissions.html

https://docs.oracle.com/javase/8/docs/api/java/nio/file/attribute/PosixFileAttributes.html

https://docs.oracle.com/javase/8/docs/api/java/nio/file/attribute/GroupPrincipal.html

然后你可以做这些事情:

Path path = Paths.get("/home/toto/myfile.zip");
FileOwnerAttributeView foav = Files.getFileAttributeView(path,
FileOwnerAttributeView.class);
FileSystem fs = FileSystems.getDefault();
UserPrincipalLookupService upls = fs.getUserPrincipalLookupService();
//UserPrincipal new_user = upls.lookupPrincipalByName("brice");     
UserPrincipal new_user = upls.lookupPrincipalByName("1005");
foav.setOwner(new_user);
GroupPrincipal new_group = upls.lookupPrincipalByGroupName("everyone");
Files.getFileAttributeView(path, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).setGroup(new_group);

最新更新