android(6)(读数据的一些权限)

读数据的权限:
public class MainActivity extends Activity implements OnClickListener {

    private String basicPath = "/data/data/com.itheima.writedata/files/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn_read_private).setOnClickListener(this);
        findViewById(R.id.btn_write_private).setOnClickListener(this);

        findViewById(R.id.btn_read_readable).setOnClickListener(this);
        findViewById(R.id.btn_write_readable).setOnClickListener(this);

        findViewById(R.id.btn_read_writeable).setOnClickListener(this);
        findViewById(R.id.btn_write_writeable).setOnClickListener(this);

        findViewById(R.id.btn_read_readable_writeable).setOnClickListener(this);
        findViewById(R.id.btn_write_readable_writeable).setOnClickListener(this);
    }

    /** * 哪一个控件被点击, v对象就代表被点击的对象 */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_read_private:
            readFile("private.txt");
            break;
        case R.id.btn_write_private:
            writeFile("private.txt");
            break;
        case R.id.btn_read_readable:
            readFile("readable.txt");
            break;
        case R.id.btn_write_readable:
            writeFile("readable.txt");
            break;
        case R.id.btn_read_writeable:
            readFile("writeable.txt");
            break;
        case R.id.btn_write_writeable:
            writeFile("writeable.txt");
            break;
        case R.id.btn_read_readable_writeable:
            readFile("readable_writeable.txt");
            break;
        case R.id.btn_write_readable_writeable:
            writeFile("readable_writeable.txt");
            break;
        default:
            break;
        }
    }

    /** * 读文件 * @param fileName */
    private void readFile(String fileName) {
        try {
            String path = basicPath + fileName;

            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
            String text = reader.readLine();
            reader.close();
            Toast.makeText(this, "读取成功: " + text, 0).show();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "读取失败: " + fileName, 0).show();
        }
    }
    /** * 写文件 * @param fileName */
    private void writeFile(String fileName) {
        try {
            String path = basicPath + fileName;

            FileOutputStream fos = new FileOutputStream(path);

            fos.write("哈哈, 被我给黑了".getBytes());

            fos.flush();

            fos.close();
            Toast.makeText(this, "写入成功: " + fileName, 0).show();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "写入失败: " + fileName, 0).show();
        }
    }
2.Linux权限:
- --- --- ---
总共十位,第一位:代表文件类型(d:文件夹;-:文件;l:挂载某一个文件)
    第2-4位:rw-:代表的是当前用户的权限。110=6.5-7位:当前用户所在组的其他组员的权限 
    第8-10位:其他所有的权限 ---没有任何权限,r--可读,-w-可写,rw-可读可写。
    改变某一个文件的权限:change mode = chmod
              chmod 666 private.txt

你可能感兴趣的:(android,数据,权限)