Android的读写文件及权限设置

设置文件生成的权限:

[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系统是基于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中修改文件的权限:

root@android:/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
root@android:/data/data/com.aaron.login/files # chmod 0000 private.txt
chmod 0000 private.txt
root@android:/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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值