14

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 ?

4

2 回答 2

11

实际上有一个“unix”视图,您可以通过以下方式访问此类特定于 Unix 的属性:

int uid = (int) Files.getAttribute(path, "unix:uid", NOFOLLOW_LINKS);
于 2013-02-15T01:14:52.370 回答
5

出于某种奇怪的原因,Java 团队拒绝记录这一点。

但是从 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");
于 2015-07-14T13:00:10.090 回答