【问题标题】:How to get File Permission Mode programmatically in Java如何在 Java 中以编程方式获取文件权限模式
【发布时间】:2012-07-09 14:59:53
【问题描述】:

我知道可以使用以下方法更改文件的权限模式:

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

此示例将权限位设置为777。是否可以使用 Java 以编程方式将权限位设置为 777?可以对每个文件都这样做吗?

【问题讨论】:

  • 也许你能在这篇文章中找到一些想法? :stackoverflow.com/questions/664432/…
  • Android 在内部使用 android.os.FileUtils,像往常一样,它对 SDK 是隐藏的。但是,如果您不想调用 #exec(..),可以使用反射访问它。

标签: java android file-permissions


【解决方案1】:

Android 很难与其他应用程序及其数据进行交互,除非通过 Intents。 Intent 不适用于权限,因为您依赖于接收 Intent 的应用程序来执行/提供您想要的;他们可能不是为了告诉任何人他们的文件权限而设计的。有一些方法可以解决它,但只有当应用程序被设计为在同一个 JVM 中运行时。 所以每个应用程序只能更改它的文件。有关文件权限的更多详细信息,请参阅http://docs.oracle.com/javase/1.4.2/docs/guide/security/permissions.html

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 它不仅不是公共 SDK API 的一部分,而且在版本高于 4.2.2 的设备中显然被删除,根据:stackoverflow.com/questions/20858972/…跨度>
    • 这不适用于 Android 6.0 (API 23) 及更高版本
    【解决方案3】:

    这是一个使用Apache Commons.IO FileUtils 的解决方案,以及File 对象上的适当方法。

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

    这相当于chmod -R 0777 /some/path。调整set{Read,Writ,Execut}able 调用以实现其他模式。 (如果有人发布适当的代码来执行此操作,我会很乐意更新此答案。)

    【讨论】:

      【解决方案4】:

      如前所述,android.os.FileUtils 已更改,Ashraf 发布的解决方案不再有效。以下方法应该适用于所有版本的 Android(尽管它确实使用反射,并且如果制造商进行了重大更改,这可能不起作用)。

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

      显然,您需要拥有该文件才能进行任何权限更改。

      【讨论】:

        猜你喜欢
        • 2012-02-07
        • 2014-07-03
        • 2011-01-29
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 1970-01-01
        • 2014-10-13
        • 2019-08-16
        相关资源
        最近更新 更多