Android基础--文件访问权限

1.Android 底层是Linux内核,因此文件访问权限与Linux中文件访问权限类似

d   rwx   rwx   rwx

文件类型 owner group other

文件类型   d 代表文件夹,-代表文件,l 代表链接

owner文件创建的用户

group 与文件创建者在同一组的其他用户

other 与文件创建者不在同一组的其他用户

Android中每一个应用都对应独立的用户,不同应用所在组是不同的,可以通过设置是两个应用在同一个组中

 

2.以下是在当前应用的私有空间内创建文件时指定不同的访问权限的Demo代码.

package com.ithiema.permission;

import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //在data/data/当前应用包名/files/目录下写入文件,MODE_PRIVATE  -rw-rw----
    public void click1(View v) {
        FileOutputStream fos = null;
        try {
            fos = openFileOutput("info1.txt", MODE_PRIVATE);
            fos.write("this is MODE_PRIVATE ".getBytes());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally{
            try {
                if(fos!=null)
                    fos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    //在data/data/当前应用包名/files/目录下写入文件, MODE_WORLD_READABLE(已过时)  -rw-rw-r--
    public void click2(View v) {
        FileOutputStream fos = null;
        try {
            fos = openFileOutput("info2.txt", MODE_WORLD_READABLE);
            fos.write("this is MODE_WORLD_READABLE ".getBytes());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally{
            try {
                if(fos!=null)
                    fos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    //在data/data/当前应用包名/files/目录下写入文件, MODE_WORLD_READABLE|MODE_WORLD_WRITEABLE(已过时)  -rw-rw-rw-
    public void click3(View v) {
        FileOutputStream fos = null;
        try {
            fos = openFileOutput("info3.txt", MODE_WORLD_READABLE|MODE_WORLD_WRITEABLE);
            fos.write("this is MODE_WORLD_READABLE|MODE_WORLD_WRITEABLE ".getBytes());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally{
            try {
                if(fos!=null)
                    fos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

}

image

posted on 2014-11-16 23:03  fujianyi  阅读(453)  评论(0编辑  收藏  举报