Android中使用chmod

Java没有像chmod这样的平台相关操作的原生支持.但是,Android通过android.os.FileUtils为其中一些操作提供实用程序. FileUtils类不是公共SDK的一部分,因此不受支持.因此,使用此风险需要您自担风险:

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);

...

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐