Test.java Source code

Java tutorial

Introduction

Here is the source code for Test.java

Source

import static java.nio.file.attribute.PosixFilePermission.GROUP_READ;
import static java.nio.file.attribute.PosixFilePermission.OWNER_READ;
import static java.nio.file.attribute.PosixFilePermission.OWNER_WRITE;

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

public class Test {

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

        Path profile = Paths.get("/user/Admin/.profile");

        PosixFileAttributes attrs = Files.readAttributes(profile, PosixFileAttributes.class);

        Set<PosixFilePermission> posixPermissions = attrs.permissions();
        posixPermissions.clear();

        String owner = attrs.owner().getName();
        String perms = PosixFilePermissions.toString(posixPermissions);
        System.out.format("%s %s%n", owner, perms);

        posixPermissions.add(OWNER_READ);
        posixPermissions.add(GROUP_READ);
        posixPermissions.add(OWNER_READ);
        posixPermissions.add(OWNER_WRITE);
        Files.setPosixFilePermissions(profile, posixPermissions);

    }
}