• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

cynchanpin

  • 博客园
  • 联系
  • 订阅
  • 管理

View Post

openFileOutput 文件属性设置、主动配置文件的可读写属性及事实上现方式

首先參考

Android 内部存储相关的函数(getCacheDir,getDir, getFileStreamPath,getFilesDir,openFileInput, ...)

1. 用openFileOutput方法改变文件的属性,可是该方法有局限性,他创建的文件仅仅能位于该程序的私有文件夹下,即/data/data/app-package/files/。


    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来实现的改变文件权限的功能。

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


 

2. 用Runtime.getRuntime().exec()调用系统命令chmod来改变文件的权限,为了能推断命令的返回值,最好写成:


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


posted on 2017-05-21 18:19  cynchanpin  阅读(1329)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3