读 写 权限


写权限:

package com.example.write;

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

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;


public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
		//创建一个私有的private.txt文件
		writeFile(this,"private.txt",this.MODE_PRIVATE);
		//创建一个可读的readable.txt文件
		writeFile(this,"readable.txt", this.MODE_WORLD_READABLE);
		
		//创建一个可写的writeable.txt文件
		writeFile(this,"writeable.txt", this.MODE_WORLD_WRITEABLE);
		
		//创建一个可读可写的rwable.txt文件
		writeFile(this,"rwable.txt", this.MODE_WORLD_READABLE + this.MODE_WORLD_WRITEABLE);
	}

	

	private void writeFile(Context context, String fileName,int mode) {
			
		//新的API函数
		try {
			FileOutputStream out = openFileOutput(fileName, mode);
		
			out.write("I am 1505".getBytes());
			
			out.flush();
			out.close();
			
		} catch (FileNotFoundException e) {
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	
}

读权限:

package com.xh.tx.read;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

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

	Button button=(Button)findViewById(R.id.btn_read_private);
	button.setOnClickListener(this);
	
	button = (Button) findViewById(R.id.btn_write_private);
	button.setOnClickListener(this);
	
	button = (Button) findViewById(R.id.btn_read_readable);
	button.setOnClickListener(this);
	
	button = (Button) findViewById(R.id.btn_write_readable);
	button.setOnClickListener(this);
	
	button = (Button) findViewById(R.id.btn_read_writeable);
	button.setOnClickListener(this);
	
	button = (Button) findViewById(R.id.btn_write_writeable);
	button.setOnClickListener(this);
	
	button = (Button) findViewById(R.id.btn_read_rw);
	button.setOnClickListener(this);
	
	button = (Button) findViewById(R.id.btn_write_rw);
	button.setOnClickListener(this);
    }
	@Override
	public void onClick(View v) {
		File privatepath = new File("/data/data/com.example.write/files/private.txt");
		File readablepath = new File("/data/data/com.example.write/files/readable.txt");
		File writeablepath = new File("/data/data/com.example.write/files/writeable.txt");
		File rwpath = new File("/data/data/com.example.write/files/rwable.txt");
		
		switch(v.getId()){
		case R.id.btn_read_private://写私有文件
		
			try {
				FileInputStream in=new FileInputStream(privatepath);
				byte[] buffer = new byte[in.available()];//available() 获取一个文件里面有多少个byte
				
				in.read(buffer);
				if(null!=buffer && buffer.length>0){
					FileOutputStream out = new FileOutputStream(privatepath);
					out.write("被日了".getBytes());
					Toast.makeText(this, "读取成功", Toast.LENGTH_SHORT).show();
					
				}
			} catch (Exception e) {
				
				Toast.makeText(this, "读取失败", Toast.LENGTH_SHORT).show();
				
				e.printStackTrace();
			}
			break;
		case R.id.btn_write_private: //读私有文件
			try {
				FileOutputStream out = new FileOutputStream(privatepath);
				
				out.write("被日了".getBytes());
				
				Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
				
			} catch (Exception e) {
				Toast.makeText(this, "写入失败", Toast.LENGTH_SHORT).show();
				e.printStackTrace();
			}
		
			break;
		case R.id.btn_read_readable: //写可写文件
			try {
				FileInputStream in = new FileInputStream(readablepath);
				byte[] buffer = new byte[in.available()]; // available() 获取一个文件里面有多少个byte
				
				in.read(buffer);
				
				if(null != buffer && buffer.length > 0)
				{
					Toast.makeText(this, "读取成功:" + new String(buffer), Toast.LENGTH_SHORT).show();
				}
				
			} catch (Exception e) {
				
				Toast.makeText(this, "读取失败", Toast.LENGTH_SHORT).show();
				e.printStackTrace();
			}
			break;
		case R.id.btn_write_readable: //写可读文件
			try {
				FileOutputStream out = new FileOutputStream(readablepath);
				
				out.write("被日了".getBytes());
				
				Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
				
			} catch (Exception e) {
				Toast.makeText(this, "写入失败", Toast.LENGTH_SHORT).show();
				e.printStackTrace();
			}
		
			break;
		case R.id.btn_read_writeable: //读可写文件
			try {
				FileInputStream in = new FileInputStream(writeablepath);
				byte[] buffer = new byte[in.available()]; // available() 获取一个文件里面有多少个byte
				
				in.read(buffer);
				
				if(null != buffer && buffer.length > 0)
				{
					FileOutputStream out = new FileOutputStream(writeablepath);
					out.write("被 日了".getBytes());
					Toast.makeText(this, "读取成功:" + new String(buffer), Toast.LENGTH_SHORT).show();
				}
				
			} catch (Exception e) {
				
				Toast.makeText(this, "读取失败", Toast.LENGTH_SHORT).show();
				e.printStackTrace();
			}
			break;
		case R.id.btn_write_writeable: //读可读文件
			try {
				FileOutputStream out = new FileOutputStream(writeablepath);
				
				out.write("被日了".getBytes());
				
				Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
				
			} catch (Exception e) {
				Toast.makeText(this, "写入失败", Toast.LENGTH_SHORT).show();
				e.printStackTrace();
			}
		
			break;
			
		case R.id.btn_read_rw: //写可读可写文件
			try {
				FileInputStream in = new FileInputStream(rwpath);
				byte[] buffer = new byte[in.available()]; // available() 获取一个文件里面有多少个byte
				
				in.read(buffer);
				
				if(null != buffer && buffer.length > 0)
				{
					FileOutputStream out = new FileOutputStream(rwpath);
					out.write("被 日了".getBytes());
					Toast.makeText(this, "读取成功:" + new String(buffer), Toast.LENGTH_SHORT).show();
				}
				
			} catch (Exception e) {
				
				Toast.makeText(this, "读取失败", Toast.LENGTH_SHORT).show();
				e.printStackTrace();
			}
			break;
		case R.id.btn_write_rw: //读可读可写文件
			try {
				FileOutputStream out = new FileOutputStream(rwpath);
				
				out.write("被日了".getBytes());
				
				Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
				
			} catch (Exception e) {
				Toast.makeText(this, "写入失败", Toast.LENGTH_SHORT).show();
				e.printStackTrace();
			}
		
			break;
			default:
				break;
		}
	}
}

布局:

<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"
    tools:context="com.xh.tx.read.MainActivity" >

    <Button 
       android:id="@+id/btn_read_private"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="读取私有文件"
       />
    <Button 
       android:id="@+id/btn_write_private"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="写取私有文件"
	/>
    <Button 
       android:id="@+id/btn_read_readable"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="读取可读文件"
       />
   <Button 
       android:id="@+id/btn_write_readable"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="写可读文件"
    />
   
      <Button 
       android:id="@+id/btn_read_writeable"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="读取可写文件"
       />
   <Button 
       android:id="@+id/btn_write_writeable"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="写取可写文件"
    />
   
      <Button 
       android:id="@+id/btn_read_rw"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="读取可读可写文件"
       />
   <Button 
       android:id="@+id/btn_write_rw"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="写取可读可写文件"
    />
</LinearLayout>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值