问题
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