文件读写权限

一、写权限
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);
}


/**
* 往、data/data/package/xx/xxx.text的文件
* param: Context 
* param1 : fileName
*/

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) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

二、读权限
@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.xh.tx.qx/files/private.txt");
File readablepath = new File("/data/data/com.xh.tx.qx/files/readable.txt");
File writeablepath = new File("/data/data/com.xh.tx.qx/files/writeable.txt");
File rwpath = new File("/data/data/com.xh.tx.qx/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)
{

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)
{

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)
{

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;
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值