设置Owner属性的三种方式 --- Owner

  • Files.setOwner()
    Path path = Paths.get("F:/cn/icer/ws/client/Business.java");
    		
    UserPrincipal owner = null;
    		
    try {
    	owner = path.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName("King");
    			
    	Files.setOwner(path, owner);
    } catch (Exception e) {
    	e.printStackTrace();
    }

  • FileOwnerAttributeView.setOwner()
    Path path = Paths.get("F:/cn/icer/ws/client/Business.java");
    		
    UserPrincipal owner = null;
    		
    FileOwnerAttributeView foav = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
    		
    try {
    	owner = path.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName("King");
    			
    	foav.setOwner(owner);
    } catch (Exception e) {
    	e.printStackTrace();
    }

  • Files.setAttribute()
    Path path = Paths.get("F:/cn/icer/ws/client/Business.java");
    		
    UserPrincipal owner = null;
    		
    try {
    	owner = path.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName("King");
    			
    	Files.setAttribute(path, "owner:owner", owner, LinkOption.NOFOLLOW_LINKS);
    } catch (Exception e) {
    	e.printStackTrace();
    }
  • lookupPrincipalByName(String name): 参数name表示系统登录名

你可能感兴趣的:(Pro,Java,7,NIO,2)