您现在的位置是:首页 > 正文

给文件设置组---POSIX

2024-04-01 07:31:08阅读 0
  • 设置用户组
    Path path = Paths.get("/home/icer/note.txt");
    
    try {
    	GroupPrincipal group = path.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByGroupName("icer");
    	Files.getFileAttributeView(path, PosixFileAttributeView.class).setGroup(group);
    } catch (IOException e) {
    	e.printStackTrace();
    }

  • 获取用户组
    Path path = Paths.get("/home/icer/note.txt");
    
    try {
    	GroupPrincipal group = (GroupPrincipal) Files.getAttribute(path, "posix:group", LinkOption.NOFOLLOW_LINKS);
    			
    	System.out.println(group.getName());
    } catch (IOException e) {
    	e.printStackTrace();
    }


网站文章