如何在Java中管理Linux文件权限? [英] How to manage Linux file permissions in Java?

查看:1372
本文介绍了如何在Java中管理Linux文件权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何获取和设置文件/目录权限的机制?

Is there any mechanism to get and set the file/directory permissions?

例如,我想以shell方式显示文件的permisssions:

For example, I want to show the permisssions of a file in a shell way:

-rwxr-xr--

是否可以使用Java执行此操作?

Is it possible to do this using Java?


  • 我知道<$>中有一些方法c $ c>文件类以了解文件 canExecute canRead canWrite ,但AFAIK此信息仅供当前用户使用。我需要知道整个八进制数,例如755,所以我需要从用户,组和其他人那里得到它。

  • 我知道Java7带来了Posix操作,但是如何可以使用较小的JRE来做到这一点吗?

  • 我不想使用像 ls 这样的命令chmod

  • I know that there are some methods in the File class to know if the file canExecute, canRead and canWrite, but AFAIK this info is for the current user only. I need to know the whole octal number, for example 755, so I need to get it from the user, from group and from others.
  • I know that Java7 brings Posix operations, but how could do this using a smaller JRE?
  • I would like not to use a command like ls, or chmod.

推荐答案

如果你可以使用外部库,那里有几个:

If you can use external libraries, there are several:

  • JPosix
  • Posix for java
  • jnr-posix

如果整个库看起来很麻烦,创建一个调用 lstat C函数的JNI包装器并返回访问模式带你大约10分钟。这是一个教程,为创建了这样一个包装器isatty ttyname 函数

If an entire library seems a hassle, creating a JNI wrapper that calls the lstat C function and returns the access mode takes you about 10 minutes. Here's a tutorial that creates such a wrapper for the isatty and ttyname functions.

这篇关于如何在Java中管理Linux文件权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆