// 要保证用户和组存在,否则lookupService会抛UserPrincipalNotFoundException UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService(); //yw 要切换的组名 GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName("yw"); //yw 要切换的用户名 UserPrincipal userPrincipal = lookupService.lookupPrincipalByName("yw"); //文件名地址 Path path = Paths.get("H:/360-phone/20201231/text.txt"); PosixFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class); view.setGroup(groupPrincipal); view.setOwner(userPrincipal);
Java 给文件设置用户和组,相当于linux命令chown
最新推荐文章于 2024-04-02 00:51:29 发布