Test.java Source code

Java tutorial

Introduction

Here is the source code for Test.java

Source

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 Test {

    public static void main(String[] args) throws Exception {
        Path path = Paths.get("home/docs/users.txt");
        listPermissions(path);
    }

    private static void listPermissions(Path path) throws Exception {
        System.out.println("Permission for " + path.getFileName());
        PosixFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class);

        PosixFileAttributes attributes = view.readAttributes();

        System.out.println("Group: " + attributes.group().getName());
        System.out.println("Owner: " + attributes.owner().getName());

        Set<PosixFilePermission> permissions = attributes.permissions();

        System.out.print("Permissions: ");
        for (PosixFilePermission permission : permissions) {
            System.out.print(permission.name() + " ");
        }
    }

}