简体   繁体   English

无法通过java代码更改文件夹的权限

[英]Can not change permission of folder through java code

I create a temporary folder and then try to change its permission in my Windows 7 machine.我创建了一个临时文件夹,然后尝试在我的 Windows 7 机器中更改其权限。 I have admin privileges.我有管理员权限。

public class FilePermissionExample
{
    public static void main( String[] args )
    {
        try {

            File file = File.createTempFile("temp", Long.toString(System.nanoTime()));

            file.delete();
            file.mkdir();

            if(file.exists()){
                System.out.println("Is Execute allow : " + file.canExecute());
                System.out.println("Is Write allow : " + file.canWrite());
                System.out.println("Is Read allow : " + file.canRead());
            }


            file.setExecutable(false);
            file.setReadable(false);
            file.setWritable(false);

            System.out.println("Is Execute allow : " + file.canExecute());
            System.out.println("Is Write allow : " + file.canWrite());
            System.out.println("Is Read allow : " + file.canRead());

            if (file.createNewFile()){
                System.out.println("File is created!");
            }else{
                System.out.println("File already exists.");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output:输出:
Is Execute allow : true是否允许执行:true
Is Write allow : true是否允许写入:true
Is Read allow : true是否允许读取:true
Is Execute allow : true是否允许执行:true
Is Write allow : true是否允许写入:true
Is Read allow : true是否允许读取:true
File already exists.文件已存在。

Expected:预期的:
Is Execute allow : true是否允许执行:true
Is Write allow : true是否允许写入:true
Is Read allow : true是否允许读取:true
Is Execute allow : false是否允许执行:false
Is Write allow : false是否允许写入:false
Is Read allow : false是否允许读取:false

I am facing a similar issue while writing hive test using hiverunner in windows.我在 Windows 中使用 hiverunner 编写 hive 测试时遇到了类似的问题。 Can anyone suggest something?任何人都可以提出建议吗?

From the API-documentation for java.io.File :来自java.io.File的 API 文档:

"A file system may implement restrictions to certain operations on the actual file-system object, such as reading, writing, and executing. These restrictions are collectively known as access permissions. The file system may have multiple sets of access permissions on a single object. For example, one set may apply to the object's owner, and another may apply to all other users. The access permissions on an object may cause some methods in this class to fail." “文件系统可能对实际文件系统对象上的某些操作实施限制,例如读取、写入和执行。这些限制统称为访问权限。文件系统可能对单个对象具有多组访问权限。例如,一个集合可能适用于对象的所有者,另一个可能适用于所有其他用户。对一个对象的访问权限可能会导致该类中的某些方法失败。”

So your os doesn't allow to change the permissions.所以你的操作系统不允许更改权限。

If you want to change the permission on an NTFS formatted drive you need to change the permissions via the AclFileAttributeView .如果要更改 NTFS 格式驱动器的权限,则需要通过AclFileAttributeView更改权限。

Below quite verbose snippet shows the principal to remove the write permission for authorized users on a given file.下面非常详细的代码段显示了删除authorized users对给定文件的write权限的主体。

Assume the user jane is owner of the file fobar.bin and user john has as authorized user write permission to it.假设用户jane是文件fobar.bin所有者,并且用户john拥有对它的authorized user写权限。 After running the snippet john could not write anymore to the file.运行代码片段后, john无法再写入文件。

static final String AUTHENTICATED_USERS = "NT AUTHORITY\\Authenticated Users";

...

Path file = Paths.get("foobar.bin");
AclFileAttributeView view = Files.getFileAttributeView(
        file, AclFileAttributeView.class);

// show current permissions for authenticated users
for (AclEntry acl : view.getAcl()) {
    if (acl.principal().getName().equals(AUTHENTICATED_USERS)) {
        System.out.printf("current permissions: %s%n", acl.permissions());
    }
}

// remove the WRITE_DATA permission for authenticated users
// get list of current ACLs
List<AclEntry> acls = view.getAcl();
for (int i = 0; i < acls.size(); i++) {
    UserPrincipal principal = acls.get(i).principal();
    String principalName = principal.getName();
    if (principalName.equals(AUTHENTICATED_USERS)) {
        // get the current permissions
        Set<AclEntryPermission> permissions = acls.get(i).permissions();
        // remove WRITE_DATA permission
        permissions.remove(AclEntryPermission.WRITE_DATA);

        // create a new ACL entry
        AclEntry entry = AclEntry.newBuilder()
                .setType(AclEntryType.ALLOW)
                .setPrincipal(principal)
                .setPermissions(permissions)
                .build();

        // replace the ACL entry for authenticated users
        acls.set(i, entry);
    }
}
// set the updated list of ACLs
view.setAcl(acls);

// show updated permissions for authenticated users
for (AclEntry acl : view.getAcl()) {
    if (acl.principal().getName().equals(AUTHENTICATED_USERS)) {
        System.out.printf("updated permissions: %s%n", acl.permissions());
    }
}

example output (long lines wrapped)示例输出(长行换行)

current permissions: [READ_NAMED_ATTRS, DELETE, EXECUTE, WRITE_ACL, \
    WRITE_ATTRIBUTES, DELETE_CHILD, WRITE_DATA, READ_ATTRIBUTES, \
    SYNCHRONIZE, WRITE_OWNER, APPEND_DATA, WRITE_NAMED_ATTRS, READ_DATA, \
    READ_ACL]
updated permissions: [READ_NAMED_ATTRS, DELETE, EXECUTE, WRITE_ACL, \
    WRITE_ATTRIBUTES, DELETE_CHILD, READ_ATTRIBUTES, SYNCHRONIZE, \
    WRITE_OWNER, APPEND_DATA, WRITE_NAMED_ATTRS, READ_DATA, READ_ACL]

the permission WRITE_DATA has been removed.权限WRITE_DATA已被删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM