设置文件权限的两种方式---POSIX

  • Files.createFile()
    Path path = Paths.get("/home/icer/note.txt");
    
    PosixFileAttributes attr = null;
    		
    try {
    	attr = Files.readAttributes(path, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
    } catch (Exception e) {
    	e.printStackTrace();
    }
    		
    FileAttribute> posixAttrs = PosixFilePermissions.asFileAttribute(attr.permissions());
    		
    try {
    	Files.createFile(path, posixAttrs);
    } catch (Exception e) {
    	e.printStackTrace();
    }

  • Files.setPosixFilePermissions()
    Path path = Paths.get("/home/icer/note.txt");
    		
    try {
    	Set permissions = PosixFilePermissions.fromString("rw-r--r--");
    		
    	Files.setPosixFilePermissions(path, permissions);
    } catch (Exception e) {
    	e.printStackTrace();
    }


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