Android(java)学习笔记184:生成 4种 不同权限的文件

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>

布局效果如下图:

Android(java)学习笔记184:生成 4种 不同权限的文件

(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,生成不同文件如下图:

Android(java)学习笔记184:生成 4种 不同权限的文件

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

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

Android(java)学习笔记184:生成 4种 不同权限的文件

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

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

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

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

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

上一篇:vue调试工具之 vue-devtools的安装


下一篇:Python sqlalchemy orm 多外键关联