如何从Java文件元数据权限中获取用户名?
如何在给定文件中使用 java 检索用户名及其对该文件的权限?
Windows 和 Linux。
How can I, throughout a given file, using java, retrieve an user name and its permissions to that file?
Windows and Linux.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
今年即将发布的 Java 7 将支持这一点。它现在以预发布格式提供。如果该级别的发布足以满足您的需求,您可以使用它。
否则,您可以采取两种不同的方法。两者都涉及与本机库的接口。
第一种是使用 Java 本机接口 (JNI) 编写与相关 Windows 和 Linux 调用交互的 C 代码。然后,您将编译 C 代码(这需要针对每个操作系统进行,并且可能针对 32 位和 64 位架构分别进行)。然后需要将生成的编译代码添加到 Java 程序的库路径中,以便可以加载它。这个选项非常高效,但也相当复杂。
第二种选择是使用 Java Native Access (JNA)。这允许编写与本机函数和结构相匹配的 Java 方法和类。 JNA 在本机库和 Java 代码之间进行编组方面发挥了很多作用。无需编写或编译任何 C 代码。这种方法的性能不如 JNI,但在许多情况下它的性能足够好。我已经在 Linux 和 64 位系统的 OS X 上实现了这种方法,这两个系统都依赖 POSIX,所以它们非常相似。我不知道 Windows 文件元数据是如何运行的,但我有理由确定它应该不会更困难。
Java 7, which will be coming out this year, will support this. It is available in a pre-release format now. If that level of release is sufficient for your needs you could use it.
Otherwise there are two different approaches you can take. Both involve interfacing with native libraries.
The first is to use Java Native Interface (JNI) to write C code that interfaces with relevant Windows and Linux calls. Then you will compile the C code (this will need to be done for each operating system, and potentially separately for 32bit and 64bit architectures). The resulting compiled code will then need to be added to your Java program's library path so that it can be loaded. This option is very performant, but also quite involved.
The second option is to use Java Native Access (JNA). This allows for writing Java methods and classes that match the native functions and structs. JNA does lots of magic to marshal between the native libraries and your Java code. There is no need to write or compile any C code. This approach is less performant than JNI, but in many situations it is sufficiently performant. I have implemented this approach for Linux and OS X for 64bit systems, both of which rely on POSIX so they are very similar. I do not know how Windows file metadata operates, but I'm reasonably certain it should not be much harder.