在Java中获取文件创建者/所有者属性

前端之家收集整理的这篇文章主要介绍了在Java中获取文件创建者/所有者属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试读取文件列表并找到创建该文件用户.使用* nix系统,您可以执行类似的操作
  1. Map<String,Object> attrs = Files.readAttributes(Paths.get(filename),"posix:*");

但是,在Windows系统上尝试它时,我收到一个错误,因为Windows无法访问POSIX属性.您可以通过执行以下操作获取“常规”(非POSIX)属性

  1. attrs = Files.readAttributes(Paths.get(filename),"*");

但该文件的创建者不包含在该列表中.

有没有办法找出谁在Windows上运行的Java程序中创建文件

解决方法

我相信你可以使用 Files.getOwner(Path,LinkOption...)来获得当前所有者(也可能是创建者)
  1. Path path = Paths.get("c:\\path\\to\\file.ext");
  2. try {
  3. UserPrincipal owner = Files.getOwner(path,LinkOption.nofollow_LINKS);
  4. String username = owner.getName();
  5. } catch (IOException e) {
  6. e.printStackTrace();
  7. }

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.

猜你在找的Java相关文章