openFileOutput 文件属性设置、主动配置文件的可读写属性及事实上现方式
2017-05-21 18:19
459 查看
首先參考
Android 内部存储相关的函数(getCacheDir,getDir, getFileStreamPath,getFilesDir,openFileInput, ...)
1. 用openFileOutput方法改变文件的属性,可是该方法有局限性,他创建的文件仅仅能位于该程序的私有文件夹下,即/data/data/app-package/files/。
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来实现的改变文件权限的功能。
2. 用Runtime.getRuntime().exec()调用系统命令chmod来改变文件的权限,为了能推断命令的返回值,最好写成:
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 }
相关文章推荐
- openFileOutput 文件属性设置、主动配置文件的可读写属性及其实现方式
- 1、怎样设置C#OpenFileDialog(文件选择窗体)的指定路径、文件格式等属性(设置打开默认路径、文件格式、窗体显示文本)
- 通过SharedPreference 去设置了配置文件,然后通过openFileInput读的时候读不到文件
- [delphi函数]FileGetAttr、FileSetAttr 读取与设置文件属性
- 【php】php.ini配置文件中output_buffering设置
- c#设置OpenFileDialog的Filter属性
- Spring.NET使用assembly方式设置配置文件
- [InstallShield]FindAllFiles与SetFileInfo配合实现文件加多文件属性设置
- .NET 配置文件设置数据库连接属性
- android 文件读写 (openFileOutput)
- 关于openFileOutput及文件存储,删除,读取简单用法
- struts配置文件中action元素属性设置解说
- CodeProject - 使用.NET配置文件appSettings元素的File属性
- android 在应用的配置文件设置apk的安装路径,以下是安装到sdcard上的方式
- java读取和设置配置文件中的属性参数
- andriod学习——Content的openFileOutput 和 openFileInput操作文件
- SharedPreferences数据、openFileOutput文件、SQLite数据库文件存储位置
- ASP.NET File.Delete只读文件引起的访问被拒绝,设置文件属性为Normal
- C# openfiledialog设置filter属性后达不到过滤效果的原因之一
- openFileOutput()对文件操作