如何获取文件权限模式编程在Java中 [英] How to get File Permission Mode programmatically in Java

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

问题描述

我知道这是可能通过更改文件的权限模式:

I know it is possible to change the permission mode of a file using:

调用Runtime.getRuntime()EXEC(搭配chmod 777 MYFILE);

本示例设置权限位 777 。是否可以设置权限位 777 编程使用Java?可以这样做的每一个文件?

This example sets the permission bits to 777. Is it possible to set the permission bits to 777 programmatically using Java? Can this be done to every file?

推荐答案

在Android中使用chmod

Using chmod in Android

Java没有像文件模式与平台相关业务的原生支持。但是,Android提供了实用工具对一些通过android.os.FileUtils这些操作。运行fileutils类不是公共SDK的一部分,因此不支持。因此,使用这种风险自负:

Java doesn't have native support for platform dependent operations like chmod. However, Android provides utilities for some of these operations via android.os.FileUtils. The FileUtils class is not part of the public SDK and is therefore not supported. So, use this at your own risk:

public int chmod(File path, int mode) throws Exception {
 Class fileUtils = Class.forName("android.os.FileUtils");
Method setPermissions =
  fileUtils.getMethod("setPermissions", String.class, int.class, int.class, int.class);
return (Integer) setPermissions.invoke(null, path.getAbsolutePath(), mode, -1, -1);
}

...
chmod("/foo/bar/baz", 0755);
...

参考:<一href="http://www.damonkohler.com/2010/05/using-chmod-in-android.html?showComment=1341900716400#c4186506545056003185">http://www.damonkohler.com/2010/05/using-chmod-in-android.html?showComment=1341900716400#c4186506545056003185

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

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