这篇文章主要介绍了如何使用java修改文件所有者及其权限,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1.设置所有者
管理文件所有者
Files.getOwner()和Files.setOwner()方法
要使用UserPrincipal来管理文件的所有者
(1)更改文件的所有者
import java.io.IOException; import java.nio.file.*; import java.nio.file.attribute.FileOwnerAttributeView; import java.nio.file.attribute.UserPrincipal; import java.nio.file.attribute.UserPrincipalLookupService; public class Main { public static void main(String[] args) { Path path = Paths.get("/www/test1.txt"); FileOwnerAttributeView foav = Files.getFileAttributeView(path, FileOwnerAttributeView.class); try { UserPrincipal owner = foav.getOwner(); System.out.format("Original owner of %s is %s%n", path, owner.getName()); FileSystem fs = FileSystems.getDefault(); UserPrincipalLookupService upls = fs.getUserPrincipalLookupService(); UserPrincipal newOwner = upls.lookupPrincipalByName("abc"); foav.setOwner(newOwner); UserPrincipal changedOwner = foav.getOwner(); System.out.format("New owner of %s is %s%n", path, changedOwner.getName()); }catch (IOException e){ e.printStackTrace(); } } }
输出
查看文件详细信息
2.ACL文件权限
Windows上支持ACL类型文件属性
使用AclFileAttributeView的
getAcl()方法获取文件的AclEntry列表
setAcl()方法设置文件的AclEntry列表
(1)读取文件e:/test1.txt的ACL条目
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.AclEntry; import java.nio.file.attribute.AclEntryPermission; import java.nio.file.attribute.AclFileAttributeView; import java.util.List; import java.util.Set; public class Main { public static void main(String[] args) { Path path = Paths.get("e:/test1.txt"); AclFileAttributeView aclView = Files.getFileAttributeView(path, AclFileAttributeView.class); if (aclView == null) { System.out.format("ACL view is not supported.%n"); return; } try { List<AclEntry> aclEntries = aclView.getAcl(); for (AclEntry entry : aclEntries) { System.out.format("Principal: %s%n", entry.principal()); System.out.format("Type: %s%n", entry.type()); System.out.format("Permissions are:%n"); Set<AclEntryPermission> permissions = entry.permissions(); for (AclEntryPermission p : permissions) { System.out.format("%s %n", p); } } } catch (IOException e) { e.printStackTrace(); } } }
输出结果为
Principal: BUILTIN\Administrators (Alias) Type: ALLOW Permissions are: WRITE_DATA WRITE_OWNER APPEND_DATA SYNCHRONIZE WRITE_ATTRIBUTES EXECUTE READ_DATA DELETE_CHILD READ_ATTRIBUTES WRITE_NAMED_ATTRS WRITE_ACL DELETE READ_ACL READ_NAMED_ATTRS Principal: NT AUTHORITY\SYSTEM (Well-known group) Type: ALLOW Permissions are: WRITE_DATA WRITE_OWNER APPEND_DATA SYNCHRONIZE WRITE_ATTRIBUTES EXECUTE READ_DATA DELETE_CHILD READ_ATTRIBUTES WRITE_NAMED_ATTRS WRITE_ACL DELETE READ_ACL READ_NAMED_ATTRS Principal: NT AUTHORITY\Authenticated Users (Well-known group) Type: ALLOW Permissions are: WRITE_DATA READ_ATTRIBUTES APPEND_DATA WRITE_NAMED_ATTRS SYNCHRONIZE WRITE_ATTRIBUTES EXECUTE DELETE READ_DATA READ_ACL READ_NAMED_ATTRS Principal: BUILTIN\Users (Alias) Type: ALLOW Permissions are: READ_ATTRIBUTES SYNCHRONIZE EXECUTE READ_DATA READ_ACL READ_NAMED_ATTRS