Home  >  Java 7

FileAttribute and PosixFilePermissions|java.nio.file.attribute API

By Arvind Rai, November 27, 2013
PosixFilePermissions and FileAttribute is JDK 7 API. FileAttribute as its name indicates keeps the file attributes and PosixFilePermissions keeps the file permissions. FileAttribute is instantiated using PosixFilePermissions.fromString() method. fromString() needs nine character string that defines the permissions.

Usage of PosixFilePermissions.fromString()

Suppose we have to provides permission as below.
Owner: READ, Write, EXECUTE
Group: READ, EXECUTE
Others:  EXECUTE
Then the 9 character permission string would like rwxr-x--x. In the string
r represents  Read
w represents Write
x represents Execute
PosixFilePermissions.asFileAttribute() method accepts file permission and returns FileAttribute.

Example of FileAttribute with PosixFilePermissions

package com.concretepage.io.file;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;

public class PosixFilePermissionsExample {
  public static void main(String[] args) {
	  Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-x--x");
      FileAttribute<Set<PosixFilePermission>> fileAttributes = PosixFilePermissions.asFileAttribute(perms);
	  System.out.println(fileAttributes.value());
  }
}
 


Output
[OTHERS_EXECUTE, GROUP_EXECUTE, OWNER_EXECUTE, GROUP_READ, OWNER_WRITE, OWNER_READ]
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us