How do I get the name of a file's owner in Java on OSX?

后端 未结 2 1931
南方客
南方客 2020-12-07 02:23

I just need to know the owner\'s name of a file on Mac using Java -- how can I go about getting it? Thanks!

2条回答
  •  半阙折子戏
    2020-12-07 03:00

    That't not possible until Java 7. With Java 7, you could use Files#getOwner() for this.

    Path          path     = Paths.get("/path/to/file.ext");
    UserPrincipal owner    = Files.getOwner(path);
    String        username = owner.getName();
    

    Prior to Java 7, your best bet is running a command or JNI in combination with some platform native code.

    See also:

    • Java 7 NIO Tutorial - Managing Metadata

提交回复
热议问题