设置文件生成的权限:

[java]  view plain copy
  1. public static boolean saveInfo(  
  2.             Context context, String userName, String userPass, int mode){  
  3.           
  4.         try {  
  5.             FileOutputStream fos;  
  6.             switch (mode) {  
  7.             case 0:  
  8.                 fos = context.openFileOutput(  
  9.                         "private.txt", Context.MODE_PRIVATE);  
  10.                 fos.write((userName+"##"+userPass).getBytes());  
  11.                 fos.close();  
  12.                 break;  
  13.             case 1:  
  14.                 fos = context.openFileOutput(  
  15.                         "readable.txt", Context.MODE_WORLD_READABLE);  
  16.                 fos.write((userName+"##"+userPass).getBytes());  
  17.                 fos.close();  
  18.                 break;  
  19.             case 2:  
  20.                 fos = context.openFileOutput(  
  21.                         "writeable.txt", Context.MODE_WORLD_WRITEABLE);  
  22.                 fos.write((userName+"##"+userPass).getBytes());  
  23.                 fos.close();  
  24.                 break;  
  25.             case 3:  
  26.                 fos = context.openFileOutput(  
  27.                         "public.txt", Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);  
  28.                 fos.write((userName+"##"+userPass).getBytes());  
  29.                 fos.close();  
  30.                 break;  
  31.             default:  
  32.                 break;  
  33.             }  
  34.               
  35.               
  36.             return true;  
  37.         } catch (Exception e) {  
  38.             e.printStackTrace();  
  39.             return false;  
  40.         }  
  41.     }  

Context.MODE_PRIVATE 私有的文件,只可以程序本身读取和修改

Context.MODE_WORLD_READABLE可读文件,外部程序可以阅读不可以修改

Context.MODE_WORLD_WRITEABLE可写文件,外部程序可以修改不可以读取

Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE外部程序可读可写


下面是读取文件和写入文件的程序代码:

读取文件:

[java]  view plain copy
  1. public void readInfo(View view){  
  2.         File file = new File("/data/data/com.aaron.login/files/public.txt");  
  3.         FileInputStream fis;  
  4.         try {  
  5.             fis = new FileInputStream(file);  
  6.             BufferedReader br = new BufferedReader(new InputStreamReader(fis));  
  7.             String result = br.readLine();  
  8.             Toast.makeText(MainActivity.this,  
  9.                     result,   
  10.                     Toast.LENGTH_LONG).show();  
  11.         } catch (Exception e) {  
  12.             // TODO Auto-generated catch block  
  13.             e.printStackTrace();  
  14.             Toast.makeText(MainActivity.this,  
  15.                     "读取文件失败",  
  16.                     Toast.LENGTH_LONG).show();  
  17.         }  
  18.     }  

写入文件:

[java]  view plain copy
  1. public void writeInfo(View view){  
  2.         File file = new File("/data/data/com.aaron.login/files/public.txt");  
  3.         FileOutputStream fos;  
  4.         try {  
  5.             fos = new FileOutputStream(file);  
  6.             fos.write("hahaha".getBytes());  
  7.             fos.close();  
  8.             Toast.makeText(MainActivity.this,  
  9.                     "写入文件成功",   
  10.                     Toast.LENGTH_LONG).show();  
  11.         } catch (Exception e) {  
  12.             // TODO Auto-generated catch block  
  13.             e.printStackTrace();  
  14.             Toast.makeText(MainActivity.this,   
  15.                     "写入文件失败",   
  16.                     Toast.LENGTH_LONG).show();  
  17.         }  
  18.           
  19.     }  


File file = new File("/data/data/com.aaron.login/files/public.txt");

同File file = new File(Context.getFileDir(), "public.txt");

若要存储文件到SD卡:File file = new File("/sdcard/info.txt");


在DDMS的File Explorer中查看文件的permission:

Android的读写文件及权限设置_第1张图片

android系统是基于linux的系统的,文件权限也是linux系统的形式:

Linux 系统下的文件权限:

位置0代表文件,d代表目录

一般情况下,android下的每一个应用程序都是一个独立的用户,对应一个独立的组

位置1-3当前用户r可读,w可写,x可执行

位置4-6当前用户所在的组r可读,w可写,x可执行

位置7-9其它用户的权限

权限对应的十进制:

- --- --- ---0 000

- rw- --- ---0 600

- rw- rw- rw-0 666



在android的adb中修改文件的权限:

[email protected]:/data/data/com.aaron.login/files # ls -l
ls -l
-rw-rw---- u0_a46   u0_a46         13 2013-08-08 08:01 private.txt
-rw-rw-rw- u0_a46   u0_a46          6 2013-08-08 09:04 public.txt
-rw-rw-r-- u0_a46   u0_a46         13 2013-08-08 08:01 readable.txt
-rw-rw--w- u0_a46   u0_a46         13 2013-08-08 08:01 writeable.txt
[email protected]:/data/data/com.aaron.login/files # chmod 0000 private.txt
chmod 0000 private.txt
[email protected]:/data/data/com.aaron.login/files # ls -l
ls -l
---------- u0_a46   u0_a46         13 2013-08-08 08:01 private.txt
-rw-rw-rw- u0_a46   u0_a46          6 2013-08-08 09:04 public.txt
-rw-rw-r-- u0_a46   u0_a46         13 2013-08-08 08:01 readable.txt
-rw-rw--w- u0_a46   u0_a46         13 2013-08-08 08:01 writeable.txt


chmod:change mode

更多相关文章

  1. Android /system/build.prop 文件
  2. android中选择文件,部分手机找不到文件路径问题的解决
  3. android取得所在位置的经纬度
  4. 自制Android下的播放器(音频来源SD卡上的固定位置)
  5. android 获取配置文件 相对路径
  6. Android关于读取临时文件

随机推荐

  1. Android中获取屏幕长宽的方法
  2. 检测android机器是否有GPS模块
  3. 仿淘宝商品列表
  4. PHP大文件分割分片上传实现代码
  5. HTTP头隐藏PHP版本号实现过程解析
  6. thinkphp诸多限制条件下如何getshell详解
  7. Nginx+php配置文件及原理解析
  8. 学漫画分镜怎么入门?画漫画分镜的技巧
  9. PHP如何防止用户重复提交表单
  10. 淘宝移动端首页的商品列表代码实现