在LINUX下每个文件都有一个权限的属性 ,那么在Android中怎么用java改变某个文件的权限呢?

Android中有两种方法可以改变文件的权限

1. 用openFileOutput方法:

FileOutputStream fos; fos = openFileOutput("filename", MODE_WORLD_READABLE);

FileOutputStream android.content.ContextWrapper .openFileOutput(String name, int mode) throws FileNotFoundException

Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.

可用的mode 参数如下:

/**
* File creation mode: the default mode, where the created file can only
* be accessed by the calling application (or all applications sharing the
* same user ID).
* @see #MODE_WORLD_READABLE
* @see #MODE_WORLD_WRITEABLE
*/
public static final int MODE_PRIVATE = 0x0000;
/**
* File creation mode: allow all other applications to have read access
* to the created file.
* @see #MODE_PRIVATE
* @see #MODE_WORLD_WRITEABLE
*/
public static final int MODE_WORLD_READABLE = 0x0001;
/**
* File creation mode: allow all other applications to have write access
* to the created file.
* @see #MODE_PRIVATE
* @see #MODE_WORLD_READABLE
*/
public static final int MODE_WORLD_WRITEABLE = 0x0002;
/**
* File creation mode: for use with {@link #openFileOutput}, if the file
* already exists then write data to the end of the existing file
* instead of erasing it.
* @see #openFileOutput
*/
public static final int MODE_APPEND = 0x8000;

其实该方法最终还是调用了系统的chmod来实现的改变文件权限的功能。

但是该方法有局限性,他创建的文件只能位于该程序的私有目录下,即/data/data/app-package/files/

2. 用Runtime.getRuntime().exec()

Runtime.getRuntime().exec("chmod 644 " + filename);

该方法调用系统命令chmod来改变文件的权限,为了能判断命令的返回值,最好写成:

Process p = Runtime.getRuntime().exec("chmod 644 " + filename); int status = p.waitFor(); if (status == 0) { //chmod succeed } else { //chmod failed }

更多相关文章

  1. 在Android Stduio 中使用requestWindowFeature(Window.FEATURE_N
  2. Android根文件系统分析
  3. 《Android学习指南》文件夹
  4. android 获取文件夹、文件的大小 以B、KB、MB、GB 为单位
  5. android下载编译以及文件系统提取总结
  6. Android禁止横竖屏和解决切换屏幕时重启Activity的方法
  7. Android 文件保存与读取
  8. Android官方DrawerLayout 抽屉式侧滑菜单-简单使用方法
  9. Android Emulator 模拟器使用方法

随机推荐

  1. php namespace命名空间详解
  2. 0810 作业 数组的键值操作
  3. 判断是否为数组的 JavaScript 方法总结
  4. php 会话跟踪
  5. PHP命名空间
  6. php 重载与事件委托
  7. 文件上传的实例
  8. 实例演绎pdo在用户登录环节是怎么防sql注
  9. php灭绝手把手教你玩文件上传
  10. mysql简单处理表格与pdo预处理的作用