文件读写权限

来源:互联网 发布:was java找文件路径 编辑:程序博客网 时间:2024/04/29 20:12

一、写权限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();}}二、读权限@Overrideprotected 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);}@Overridepublic 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() 获取一个文件里面有多少个bytein.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() 获取一个文件里面有多少个bytein.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() 获取一个文件里面有多少个bytein.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() 获取一个文件里面有多少个bytein.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;}}


0 0
原创粉丝点击