开发者

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

开发者 https://www.devze.com 2023-03-15 05:20 出处:网络
I can use the following code to get the name of the owner of a file; final PosixFileAttributes basicFileAttributes =

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

    final PosixFileAttributes basicFileAttributes =
        Fil开发者_如何学JAVAes.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 ?


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);


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");
0

精彩评论

暂无评论...
验证码 换一张
取 消