有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    可以在JDK 7中使用新的IO API(Java NIO)实现这一点

    有getOwner()/setOwner()方法可用于管理文件的所有权,也可以使用PosixFileAttributeView.setGroup()来管理组

    下面的代码片段显示了如何使用setOwner方法设置文件所有者:

    Path file = ...;
    UserPrincipal owner = file.GetFileSystem().getUserPrincipalLookupService()
            .lookupPrincipalByName("sally");
    Files.setOwner(file, owner);
    

    Files类中没有用于设置组所有者的专用方法。但是,直接执行此操作的安全方法是通过POSIX文件属性视图,如下所示:

    Path file = ...;
    GroupPrincipal group =
        file.getFileSystem().getUserPrincipalLookupService()
            .lookupPrincipalByGroupName("green");
    Files.getFileAttributeView(file, PosixFileAttributeView.class)
         .setGroup(group);