gpt4 book ai didi

java - 如何在Windows上使用java NIO2设置文件权限?

转载 作者:行者123 更新时间:2023-12-02 09:29:32 24 4
gpt4 key购买 nike

在 Windows 上使用 java8 NIO2 设置文件权限是否有与此不同的方法?

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

最佳答案

设置各种属性的File方法:setExecutablesetReadablesetReadOnlysetWritableFiles 方法取代 setAttribute(Path, String, Object, LinkOption...) .

使用示例:

public void setFileAttributes() throws IOException {
Path path = ...

UserPrincipal user = path.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName("user");
AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class);
AclEntry entry = AclEntry.newBuilder()
.setType(ALLOW)
.setPrincipal(user)
.setPermissions(Set.of(READ_DATA, EXECUTE, WRITE_DATA))
.build();
List<AclEntry> acl = view.getAcl();
acl.add(0, entry);

Files.setAttribute(path, "acl:acl", acl);
}

参见AclFileAttributeView了解更多详情。

关于java - 如何在Windows上使用java NIO2设置文件权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58096912/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com