Add a set of attributes when creating directory - Java File Path IO

Java examples for File Path IO:File Operation

Description

Add a set of attributes when creating directory

Demo Code

import java.io.IOException;
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) {
    Path newdir = FileSystems.getDefault().getPath(
        "/home/folder1/folder2/2010/");

    Set<PosixFilePermission> perms = PosixFilePermissions
        .fromString("rwxr-x---");
    FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions
        .asFileAttribute(perms);/*from w  w w  . j av  a 2  s  . c  o m*/
    try {
      Files.createDirectory(newdir, attr);
    } catch (IOException e) {
      System.err.println(e);
    }

  }
}

Related Tutorials