How to get File Permission Mode programmatically in Java

你离开我真会死。 提交于 2019-12-29 08:51:29

问题


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

Runtime.getRuntime().exec( "chmod 777 myfile" );.

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?


回答1:


Using chmod in Android

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

Reference : http://www.damonkohler.com/2010/05/using-chmod-in-android.html?showComment=1341900716400#c4186506545056003185




回答2:


Android make it difficult to interact with other applications and their data except through Intents. Intents won't work for permissions because you're dependent on the application receiving the Intent to do/provide what you want; they probably weren't designed to tell anybody their files' permissions. There are ways around it, but only when the applications are desgned to operate in the same JVM. So each application can only change it's file. for more detail in file permission see http://docs.oracle.com/javase/1.4.2/docs/guide/security/permissions.html




回答3:


As commented previously, android.os.FileUtils has changed and the solution posted by Ashraf no longer works. The following method should work on all versions of Android (although it does use reflection and if a manufacturer made major changes this may not work).

public static void chmod(String path, int mode) throws Exception {
    Class<?> libcore = Class.forName("libcore.io.Libcore");
    Field field = libcore.getDeclaredField("os");
    if (!field.isAccessible()) {
        field.setAccessible(true);
    }
    Object os = field.get(field);
    Method chmod = os.getClass().getMethod("chmod", String.class, int.class);
    chmod.invoke(os, path, mode);
}

Obviously you will need to own the file to make any permission changes.




回答4:


Here is a solution using Apache Commons.IO FileUtils, and the appropriate methods on File objects.

for (File f : FileUtils.listFilesAndDirs(new File('/some/path'), TrueFileFilter.TRUE, TrueFileFilter.TRUE)) {
    if (!f.setReadable(true, false)) { 
        throw new IOException(String.format("Failed to setReadable for all on %s", f));
    }
    if (!f.setWritable(true, false)) {
        throw new IOException(String.format("Failed to setWritable for all on %s", f));
    }
    if (!f.setExecutable(true, false)) { 
        throw new IOException(String.format("Failed to setExecutable for all on %s", f));
    }
}

This is the equivalent of chmod -R 0777 /some/path. Adjust the set{Read,Writ,Execut}able calls to implement other modes. (I'll happily update this answer if someone posts appropriate code to do this.)



来源:https://stackoverflow.com/questions/11408154/how-to-get-file-permission-mode-programmatically-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!