How to get numeric groupid/userid using java7 file attribute apis?

社会主义新天地 提交于 2020-12-02 06:04:21

问题


I can use the following code to get the name of the owner of a file;

    final PosixFileAttributes basicFileAttributes =
        Files.readAttributes( path, PosixFileAttributes.class, 
                                    LinkOption.NOFOLLOW_LINKS );
    String ownerName = basicFileAttributes.owner().getName();

But I'm also trying to get hold of the numeric unix id of the user in question. In the debugger I can see it's hiding inside "UnixFileAttributes" (subclass of PosixFileAttributes), but is there any reasonably standard way to get hold of it ?


回答1:


There's actually a "unix" view you can get access to such Unix-specific attributes through:

int uid = (int) Files.getAttribute(path, "unix:uid", NOFOLLOW_LINKS);



回答2:


For some strange reason the Java team refuses to document this.

But from jdk/test/java/nio/file/Files/FileAttributes.java...

int mode = (Integer)Files.getAttribute(file, "unix:mode");
long ino = (Long)Files.getAttribute(file, "unix:ino");
long dev = (Long)Files.getAttribute(file, "unix:dev");
long rdev = (Long)Files.getAttribute(file, "unix:rdev");
int nlink = (Integer)Files.getAttribute(file, "unix:nlink");
int uid = (Integer)Files.getAttribute(file, "unix:uid");
int gid = (Integer)Files.getAttribute(file, "unix:gid");
FileTime ctime = (FileTime)Files.getAttribute(file, "unix:ctime");
map = Files.readAttributes(file, "unix:*");
map = Files.readAttributes(file, "unix:size,uid,gid");


来源:https://stackoverflow.com/questions/6446508/how-to-get-numeric-groupid-userid-using-java7-file-attribute-apis

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!