文件读写权限
来源:互联网 发布: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
- vc_文件读写权限
- android文件读写权限
- ubuntu读写文件权限
- android读写文件权限
- 文件读写权限更改
- linux 文件读写权限
- 文件读写权限
- Linux文件读写权限
- Android的读写文件权限
- 文件读写的参数 权限
- Android的读写文件权限
- nodejs-修改文件读写权限
- mac 文件的读写权限
- 给android系统文件加读写权限
- Android的读写文件及权限设置
- linux下java设置文件读写权限
- 权限导致对hdfs文件读写错误
- Python 查看文件的读写权限
- 赫夫曼树与赫夫曼编码
- 初学Android,字符串,数字,尺寸,数组资源(十二)
- iOS开发25-iOS可视化编程-Interface Builder
- 四元数的转换(一)
- 我的Java学习笔记1——java执行Oracle的sql语句时报错ORA-00911无效字符错误
- 文件读写权限
- Android监听自身卸载,弹出用户反馈调查
- 在CentOS7.0安装Orchestration模块(heat)
- IEnumerator 和IEnumerable,及其泛型
- SpringMVC @RequestBody接收Json对象字符串
- android,2048
- 开关三极管的使用误区
- XMPP客户端库Smack
- perl 自动登录盈盈理财