Android 内部存储文件读写权限

来源:互联网 发布:淘宝刷单好评 编辑:程序博客网 时间:2024/05/18 00:39

Android权限机制
* MODE_PRIVATE 私有文件
* MODE_WORLD_READABLE 全局可读文件
* MODE_WORLD_WRITEABLE 全局可写文件
* MODE_WORLD_READABLE + MODE_WORLD_WRITEABLE 全局可读可写

这里写图片描述

<LinearLayout 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"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <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="私有文件" />        <RadioButton            android:id="@+id/rb_readonly"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="全局可读文件" />        <RadioButton            android:id="@+id/rb_writeonly"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="全局可写文件" />        <RadioButton            android:id="@+id/rb_readwrite"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="全局可读可写文件" />    </RadioGroup>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="createFile"        android:text="创建文件" /></LinearLayout>
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);        //  读取数据        FileInputStream fis;        try {            fis = new FileInputStream(new File("/data/data/com.cy.readwrite/files/private.txt"));            BufferedReader br = new BufferedReader(new InputStreamReader(fis));            String line = br.readLine();            System.out.println("===================" + line);        } catch (Exception e) {            System.out.println("读取失败!");            e.printStackTrace();        }    }    public void createFile(View view){        // 点击事件        int id = rg_mode.getCheckedRadioButtonId();        try {            FileOutputStream fos = null;            switch (id) {                case R.id.rb_private:                    System.out.println("创建私有文件");                    fos = openFileOutput("private.txt", MODE_PRIVATE);                    fos.write("gagagagaga!!!!".getBytes());                    break;                case R.id.rb_readonly:                    System.out.println("创建全局可读文件");                    fos = openFileOutput("readonly.txt", MODE_WORLD_READABLE);                    fos.write("gagagagaga!!!!".getBytes());                    break;                case R.id.rb_writeonly:                    System.out.println("创建全局可写文件");                    fos = openFileOutput("writeonly.txt", MODE_WORLD_WRITEABLE);                    fos.write("gagagagaga!!!!".getBytes());                    break;                case R.id.rb_readwrite:                    System.out.println("创建全局可读可写文件");//                  /data/data                    fos = openFileOutput("readwrite.txt", MODE_WORLD_READABLE + MODE_WORLD_WRITEABLE);                    fos.write("gagagagaga!!!!".getBytes());                    break;                default:                    break;            }            if(fos != null){                // 关闭流                fos.close();            }        } catch (Exception e) {            e.printStackTrace();            Toast.makeText(this, "文件创建失败", 0).show();        }    }}

参考:Android 存储

原创粉丝点击