Android 文件的权限,以及跨应用读取文件

	//点击按钮 生成一个私有的文件 
	public void click1(View v){
		
		try {
			FileOutputStream fos = openFileOutput("private.txt", MODE_PRIVATE);
//			fos.write("private".getBytes());
			fos.write("haha".getBytes());
			
			
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	//点击按钮   使用MODE_APPEND模式  生成一个append.txt文件 依然是私有,可以用append模式向后添加数据而不是覆盖原文本数据 
		public void click2(View v){
			try {
				FileOutputStream fos = openFileOutput("append.txt", MODE_APPEND);
//				fos.write("append".getBytes());
				fos.write("hehe".getBytes());
				fos.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		
		//点击按钮   使用MODE_WORLD_READABLE 模式  生成一个read.txt 文件  在Android7.0版本被禁用

		public void click3(View v){
			try {
				FileOutputStream fos = openFileOutput("read.txt", MODE_WORLD_READABLE);
				fos.write("read".getBytes());
				fos.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
	
		
		
		//点击按钮  使用这个模式MODE_WORLD_WRITEABLE  生成一个write.txt文件 在Android7.0版本被禁用
				public void click4(View v){
					try {
						FileOutputStream fos = openFileOutput("write.txt", MODE_WORLD_WRITEABLE);
						fos.write("write".getBytes());
						fos.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
					
				}


在另一个App里读取上一个App文件内的数据,在Android7.0版本被禁用

        try {
            File file = new File("data/data/com.example.a14_file_permission/files/write.txt");
            FileOutputStream fos = new FileOutputStream(file);
            BufferedWriter bufr = new BufferedWriter(new OutputStreamWriter(fos));

            String content = "content";
            bufr.write(content);
            bufr.close(); //流用完要关闭,刚才没关闭流,在流缓冲里的content没写进去。

            Log.w("JustTest", content);
        } catch (Exception e) {
            e.printStackTrace();
        }

以上测试在Android4.0.3版本进行。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值