1. 首先我们编写一个生成 4种 不同权限的文件的程序案例:

(1)首先是activity_main.xml文件:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" > <Button
android:onClick="click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="生成文件" /> <RadioGroup
android:id="@+id/rg_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <RadioButton
android:id="@+id/rb_private"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="私有 private" /> <RadioButton
android:id="@+id/rb_readble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全局可读 world readable" /> <RadioButton
android:id="@+id/rb_writeable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全局可写world writeable" /> <RadioButton
android:id="@+id/rb_public"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全局可读可写public" />
</RadioGroup> </RelativeLayout>

布局效果如下图:

(2)接下来是代码逻辑部分如下:MainActivity.java:

 package com.itheima.filemode;

 import java.io.FileOutputStream;

 import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup; public class MainActivity extends Activity {
private RadioGroup rg_mode; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg_mode = (RadioGroup) findViewById(R.id.rg_mode);
} /**
* 按钮的点击事件
*
* @param view
*/
public void click(View view) {
try {
int id = rg_mode.getCheckedRadioButtonId();
FileOutputStream fos = null;
switch (id) {
case R.id.rb_private:// 私有文件
fos = openFileOutput("private.txt", MODE_PRIVATE);
break;
case R.id.rb_public:// 可读可写文件
fos = openFileOutput("public.txt", MODE_WORLD_READABLE+MODE_WORLD_WRITEABLE);
break;
case R.id.rb_readble:// 全局可读文件
fos = openFileOutput("readble.txt", MODE_WORLD_READABLE);
break;
case R.id.rb_writeable:// 全局可写文件
fos = openFileOutput("writeable.txt", MODE_WORLD_WRITEABLE);
break;
}
fos.write("dfafda".getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

openFileOutput()方法的第一参数用于指定文件名称,不能包含路径分隔符“/” ,如果文件不存在,Android 会自动创建它。创建的文件保存在/data/data/<package name>/files目录,如: /data/data/cn.itcast.action/files/itcast.txt ,通过点击Eclipse菜单“Window”-“Show View”-“Other”,在对话窗口中展开android文件夹,选择下面的File Explorer视图,然后在File Explorer视图中展开/data/data/<package name>/files目录就可以看到该文件。

我们分别点击界面上不同的RadioButton,生成不同文件如下图:

2. 小结:android下文件访问的权限:

* 默认情况下所有的文件创建出来都是私有的。只有自己的应用程序可以访问里面的数据,别的应用程序是不可以访问数据的。
* 特殊情况利用api可以修改文件的权限。
      openFileOutput("文件名","文件的访问模式"); 私有 只读 只写 可读可写
* 底层是通过Linux操作系统的文件模式来实现的。

Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中。可以使用Context.MODE_APPEND

Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。

Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。

MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;

MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。

最新文章

  1. Jquery中ajax方法data参数的用法
  2. JavaScript数组定义
  3. [Hyper-V]使用操作系统模板创建新的虚拟机
  4. QQ空间分享功能(二)
  5. Linux查看用户登陆历史记录
  6. (转载)OC学习篇之---Foundation框架中的NSDirctionary类以及NSMutableDirctionary类
  7. Jersey(1.19.1) - Conditional GETs and Returning 304 (Not Modified) Responses
  8. ArcGIS Engine实现LAS数据集转RASTER
  9. 关于 Boolean 的转换
  10. Kernel-Scheduler implementation
  11. gzip优化网络传输量提高传输效率[转]
  12. await与async的简单了解
  13. Activity 的生命周期
  14. spring框架的IOC的底层原理
  15. 读了前半本&lt;Thinking in Java&gt;
  16. 与信号相关的linux系统编程API
  17. 不用安装Oracle客户端
  18. Laravel 5.6 模型关联 user 表后查询 user 表数据只能获取第一条数据,不知道怎么获取第二条
  19. 嵌入式常用技术概览之SPI
  20. java类加载器 Bootstrap、ExtClassLoader、AppClassLoader的关系

热门文章

  1. Centos7.2 安装配置 Tengine(nginx)
  2. 在ubuntu下如何验证文件的MD5码 (转载)
  3. MVC用户验证
  4. Scipy的应用
  5. 51nod1117(简单huffman tree)
  6. spring oauth2.0 实现原理
  7. jar包冲突问题
  8. Codeforces Round #562 (Div. 2) A.Circle Metro
  9. hdu2027 trie树 字典树模板
  10. maven settings.xml windows