在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. 浅谈Java中Collections.sort对List排序的两种方法
  2. NPM 和webpack 的基础使用
  3. Python list sort方法的具体使用
  4. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  5. python list.sort()根据多个关键字排序的方法实现
  6. Android(安卓)Log系统介绍 (基于Android(安卓)N)
  7. 在Android(安卓)Stduio 中使用requestWindowFeature(Window.FEAT
  8. Android根文件系统分析
  9. 《Android学习指南》文件夹

随机推荐

  1. 2013.12.04 (4)——— android SlidingMenu
  2. 两个界面的切换
  3. Local Caching of Remote Images in AIR
  4. Android应用程序键盘(Keyboard)消息处理机
  5. Android中直接按路径读取properties文件
  6. 启动虚拟机出现cannot launch AVD in emu
  7. LitePal框架上手小记录
  8. 从android模拟器中提取文件系统
  9. Android之手机屏幕朝向
  10. 《android 利用自带技术解析json字符》