设置文件生成的权限:

public static boolean saveInfo(
Context context, String userName, String userPass, int mode){ try {
FileOutputStream fos;
switch (mode) {
case 0:
fos = context.openFileOutput(
"private.txt", Context.MODE_PRIVATE);
fos.write((userName+"##"+userPass).getBytes());
fos.close();
break;
case 1:
fos = context.openFileOutput(
"readable.txt", Context.MODE_WORLD_READABLE);
fos.write((userName+"##"+userPass).getBytes());
fos.close();
break;
case 2:
fos = context.openFileOutput(
"writeable.txt", Context.MODE_WORLD_WRITEABLE);
fos.write((userName+"##"+userPass).getBytes());
fos.close();
break;
case 3:
fos = context.openFileOutput(
"public.txt", Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
fos.write((userName+"##"+userPass).getBytes());
fos.close();
break;
default:
break;
} return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

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

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

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

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

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

读取文件:

public void readInfo(View view){
File file = new File("/data/data/com.aaron.login/files/public.txt");
FileInputStream fis;
try {
fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String result = br.readLine();
Toast.makeText(MainActivity.this,
result,
Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(MainActivity.this,
"读取文件失败",
Toast.LENGTH_LONG).show();
}
}

写入文件:

public void writeInfo(View view){
File file = new File("/data/data/com.aaron.login/files/public.txt");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
fos.write("hahaha".getBytes());
fos.close();
Toast.makeText(MainActivity.this,
"写入文件成功",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(MainActivity.this,
"写入文件失败",
Toast.LENGTH_LONG).show();
} }

在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

Android的读写文件权限的更多相关文章

  1. 在android中读写文件

    在android中读写文件 android中只有一个盘,正斜杠/代表根目录. 我们常见的SDK的位置为:/mnt/sdcard 两种最常见的数据存储方式: 一.内存 二.本地 1.手机内部存储 2.外 ...

  2. Android的读写文件及权限设置

    drwx read write excute openFileOutput(name,drwx); 用系统api读取文件 设置文件生成的权限:    public static boolean sav ...

  3. Android中的文件权限操作

    默认本工程创建的文件本工程对其有读写权限. 我们可以通过context.openFileOutput("文件名", 模式): 我们可以创建私有, 共有, 只读, 只写文件, 默认的 ...

  4. Deepin-安装(读写文件)权限

    在安装NODE管理模块N时,遇到了权限问题 1.给予程序读写权限(仅限文件夹) 查看权限:ls -l 或 ls 添加权限: 示例:chmod +rw xx 实例:chmod +rw node 关于权限 ...

  5. android c 读写文件

    1.包含头文件 #include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<fcn ...

  6. Android -- 读写文件到内部ROM,SD卡,SharedPreferences,文件读写权限

    (内容整理自张泽华教程) 1. 概述 使用文件进行数据存储 首先给大家介绍使用文件如何对数据进行存储,Activity提供了openFileOutput()方法可以用于把数据输出到文件中,具体的实现过 ...

  7. android 学习随笔二(读写文件)

    在android读写文件 RAM:运行内存,相当于电脑的内存 ROM:内部存储空间,相当电脑硬盘,android手机必须有的 SD卡:外部存储空间,相当电脑的移动硬盘,不是必须的.手机如果内置16G存 ...

  8. Android AndroidManifest 清单文件以及权限具体解释

    每一个Android应用都须要一个名为AndroidManifest.xml的程序清单文件,这个清单文件名称是固定的而且放在每一个Android应用的根文件夹下.它定义了该应用对于Android系统来 ...

  9. android 写文件权限

    首先,在manifest.xml中添加user permission:<uses-permission android:name="android.permission.WRITE_E ...

随机推荐

  1. expect set timeout -1 永不超时

    . ~/.bash_profile passwd='xxx' expect <<! set timeout -1 spawn rsync -avH /webapps/Seeyon/A8/b ...

  2. 行变列 pivot

    SELECT p.City , [临时] , [会员] , VIPFROM ( SELECT c.CustomerID , c.City , CASE WHEN COUNT(o.OrderID) &l ...

  3. 理解Servlet及其对象

    一.ServeltConfig对象 在Servlet的配置文件中,可以使用一个或多个<init-param>标签为Servlet配置一些初始化参数.而不当Servlet配置了初始化参数后, ...

  4. python 笔记2--函数

    函数变量 >>> a = abs # 变量a指向abs函数 >>> a(-1) # 所以也可以通过a调用abs函数 1 定义函数 def my_abs(x): if ...

  5. 【高精度+DP】【HDU1223】 OrderCount

    Order Count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  6. android 数据存储之SharePreference 的几种方式

    1. 常见的 getSharedPreferences(String filename,mode) 指定sp文件的名称,生成的文件名为 filename.xml 2.getPreferences(mo ...

  7. [转]C# 中的.pdb/ .vshost.exe/ .vshost.exe.manifest文件讨论

    原文出处:http://blog.163.com/chwei_sunshine/blog/static/19412628320125893656652/ pdb文件: 英文全称:Program Dat ...

  8. Best Time to Buy and Sell Stock (java)

    有一组数组代表股票的价格 一次买一次卖如何得到最大利润? public int maxProfit(int[] prices) { if(prices.length==0)return 0; int ...

  9. JDBC_mysql---防sql注入,存储图片

    package PreparedStatement_sql注入; import java.io.File; import java.io.FileInputStream; import java.io ...

  10. HDU 1071 - The area

    求曲线和直线围成的面积 求表达式,求积分 #include <iostream> using namespace std; ],y[]; int t; double k,m;//fx1: ...