一、写权限
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;
}
}
文件读写权限
最新推荐文章于 2024-09-04 20:57:54 发布