Creating a Directory By using the invoking the PosixFilePermission() method to set permission - Java File Path IO

Java examples for File Path IO:Directory Create

Description

Creating a Directory By using the invoking the PosixFilePermission() method to set permission

Demo Code

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
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) {
    FileSystem fileSystem = FileSystems.getDefault();
    Path directory = fileSystem.getPath("./newDirectoryWPermissions");
    try {//  ww w .  jav  a2s .  com
      Set<PosixFilePermission> perms = PosixFilePermissions
          .fromString("rwxr-x---");
      FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions
          .asFileAttribute(perms);
      Files.createDirectory(directory, attr);

    } catch (IOException e) {
      e.printStackTrace();
    }

  }

}

Related Tutorials