Managing POSIX attributes - Java File Path IO

Java examples for File Path IO:Unix File

Description

Managing POSIX attributes

Demo Code

import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;

public class Main {
  public static void main(String[] args) throws Exception {
    Path path = Paths.get("home/docs/users.txt");
    PosixFileAttributeView view = Files.getFileAttributeView(path,
        PosixFileAttributeView.class);

    PosixFileAttributes attributes = view.readAttributes();
    Set<PosixFilePermission> permissions = attributes.permissions();
    listPermissions(permissions);//from www  .  j  a v a  2s.  c  o  m

    permissions.add(PosixFilePermission.OTHERS_WRITE);
    view.setPermissions(permissions);

    System.out.println();
    listPermissions(permissions);
  }

  private static void listPermissions(Set<PosixFilePermission> permissions) {
    System.out.print("Permissions: ");
    for (PosixFilePermission permission : permissions) {
      System.out.print(permission.name() + " ");
    }
    System.out.println();
  }

}

Related Tutorials