Get the file creator / owner attribute in Java
•
Java
I'm trying to read the file list and find the user who created the file With the * Nix system, you can perform similar operations
Map<String,Object> attrs = Files.readAttributes(Paths.get(filename),"posix:*");
However, when trying it on a Windows system, I received an error because windows could not access POSIX properties You can get general (non POSIX) attributes by doing the following:
attrs = Files.readAttributes(Paths.get(filename),"*");
However, the creator of the file is not included in the list
Is there any way to find out who creates files in Java programs running on windows?
Solution
I believe you can use files getOwner(Path,LinkOption...) To get the current owner (or possibly the creator)
Path path = Paths.get("c:\\path\\to\\file.ext"); try { UserPrincipal owner = Files.getOwner(path,LinkOption.nofollow_LINKS); String username = owner.getName(); } catch (IOException e) { e.printStackTrace(); }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码