Java IO Tutorial - Java PosixFilePermissions .asFileAttribute (Set < PosixFilePermission > perms)








Syntax

PosixFilePermissions.asFileAttribute(Set < PosixFilePermission > perms) has the following syntax.

public static FileAttribute < Set < PosixFilePermission >> asFileAttribute(Set < PosixFilePermission > perms)

Example

In the following code shows how to use PosixFilePermissions.asFileAttribute(Set < PosixFilePermission > perms) method.

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;
//from   ww w.j  ava 2  s  . c om
public class Main {

  public static void main(String[] args) throws Exception {

    Path path = Paths.get("c:/home/tutorial/Java/JavaFX/Topic.txt");
    Path new_path = Paths.get("/home/tutorial/Java/JavaFX/new_Topic.txt");
    PosixFileAttributes attr = Files.readAttributes(path,PosixFileAttributes.class);
    attr = Files.getFileAttributeView(path, PosixFileAttributeView.class)
        .readAttributes();

    //use of asFileAttribute
    FileAttribute<Set<PosixFilePermission>> posixattrs = PosixFilePermissions.asFileAttribute(attr.permissions());
    try {
        Files.createFile(new_path, posixattrs);
    } catch (IOException e) {
        System.err.println(e);
    }
  }
}