我正在尝试读取文件列表并找到创建该文件的用户.使用* nix系统,您可以执行类似的操作
- Map<String,Object> attrs = Files.readAttributes(Paths.get(filename),"posix:*");
但是,在Windows系统上尝试它时,我收到一个错误,因为Windows无法访问POSIX属性.您可以通过执行以下操作获取“常规”(非POSIX)属性:
- attrs = Files.readAttributes(Paths.get(filename),"*");
但该文件的创建者不包含在该列表中.
有没有办法找出谁在Windows上运行的Java程序中创建文件?
解决方法
我相信你可以使用
Files.getOwner(Path,LinkOption...)
来获得当前所有者(也可能是创建者)
- 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();
- }
This should work if it is a file system that supports 07001. This file attribute view provides access to a file attribute that is the owner of the file.