Java PosixFilePermission convert to file attribute

Description

Java PosixFilePermission convert to file attribute

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;

public class Main {

   public static void main(String[] args) {
      Path path = Paths.get("home/docs/users.txt");
      try {/*ww w.  j a  v  a 2  s .c o m*/
         PosixFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class);

         PosixFileAttributes attributes = view.readAttributes();
         Set<PosixFilePermission> permissions = attributes.permissions();

         FileAttribute<Set<PosixFilePermission>> fileAttributes = PosixFilePermissions.asFileAttribute(permissions);
         Set<PosixFilePermission> fileAttributeSet = fileAttributes.value();
         for (PosixFilePermission posixFilePermission : fileAttributeSet) {
            System.out.print(posixFilePermission.toString() + ' ');
         }

         System.out.println(PosixFilePermissions.toString(permissions));
      } catch (IOException ex) {
      }

   }

}



PreviousNext

Related