Java Path File Read nio setReadable(final Path path, boolean readable)

Here you can find the source of setReadable(final Path path, boolean readable)

Description

set Readable

License

LGPL

Declaration

public static void setReadable(final Path path, boolean readable) throws IOException 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.AclEntry;
import java.nio.file.attribute.AclEntryPermission;
import java.nio.file.attribute.AclEntryType;
import java.nio.file.attribute.AclFileAttributeView;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.UserPrincipal;
import java.util.List;
import java.util.Locale;
import java.util.Set;

public class Main {
    public static void setReadable(final Path path, boolean readable) throws IOException {
        if (isWindows()) {
            // Set permissions using DOS

            final UserPrincipal currentUser = path.getFileSystem().getUserPrincipalLookupService()
                    .lookupPrincipalByName(System.getProperty("user.name"));

            // get view
            final AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class);

            // create ACE to set read access for the current user
            final AclEntryType entryType = readable ? AclEntryType.ALLOW : AclEntryType.DENY;
            final AclEntry entry = AclEntry.newBuilder().setType(entryType).setPrincipal(currentUser)
                    .setPermissions(AclEntryPermission.READ_DATA, AclEntryPermission.READ_ATTRIBUTES).build();

            // read ACL, insert ACE, re-write ACL
            final List<AclEntry> acl = view.getAcl();
            acl.add(0, entry); // insert before any DENY entries
            view.setAcl(acl);//  ww w  . j  av  a2  s .c om

        } else {
            // Set permissions using POSIX
            final Set<PosixFilePermission> perms = Files.getPosixFilePermissions(path);
            if (readable) {
                perms.add(PosixFilePermission.OWNER_READ);
                perms.add(PosixFilePermission.GROUP_READ);
                perms.add(PosixFilePermission.OTHERS_READ);
            } else {
                perms.remove(PosixFilePermission.OWNER_READ);
                perms.remove(PosixFilePermission.GROUP_READ);
                perms.remove(PosixFilePermission.OTHERS_READ);
            }
            Files.setPosixFilePermissions(path, perms);
        }
    }

    public static boolean isWindows() {
        return System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows");
    }
}

Related

  1. readSheBang(Path script)
  2. readStringFromPath(Path path)
  3. readTextFile(Path p)
  4. readTextFileFromResources(String filepath, ClassLoader classLoader)
  5. readTextFromFile(String textFilePath)
  6. setReadOnly(Path file)