android,保存文件到内存和读取,文件访问权限,文件模式

来源:互联网 发布:国税部门知乎 编辑:程序博客网 时间:2024/06/07 07:12
/** 上下文对象,要初始化 */Context mContext;/** 私有模式 */public static final int FIlE_MODE_PRIVATE = 0;/** 可读模式 */public static final int FIlE_MODE_READABLE = 1;/** 可写模式 */public static final int FIlE_MODE_WRITEABLE = 2;/** 公开模式 */public static final int FIlE_MODE_PUBLIC = 3;/** * 保存文件到手机内存 *  * @param name *            文件名 * @param content *            文件内容 * @param append *            是否追加 * @param mode *            文件模式。私有,可读,可写,公开。 * @return 保存是否成功 */public boolean saveFileToROM(String name, String content, boolean append,int mode){try{FileOutputStream fos = null;switch (mode){case FIlE_MODE_PRIVATE:if (append){fos = mContext.openFileOutput(name, Context.MODE_PRIVATE+ Context.MODE_APPEND);} else{fos = mContext.openFileOutput(name, Context.MODE_PRIVATE);}break;case FIlE_MODE_READABLE:if (append){fos = mContext.openFileOutput(name,Context.MODE_WORLD_READABLE + Context.MODE_APPEND);} else{fos = mContext.openFileOutput(name,Context.MODE_WORLD_READABLE);}break;case FIlE_MODE_WRITEABLE:if (append){fos = mContext.openFileOutput(name,Context.MODE_WORLD_WRITEABLE + Context.MODE_APPEND);} else{fos = mContext.openFileOutput(name,Context.MODE_WORLD_WRITEABLE);}break;case FIlE_MODE_PUBLIC:if (append){fos = mContext.openFileOutput(name,Context.MODE_WORLD_READABLE+ Context.MODE_WORLD_WRITEABLE+ Context.MODE_APPEND);} else{fos = mContext.openFileOutput(name,Context.MODE_WORLD_READABLE+ Context.MODE_WORLD_WRITEABLE);}break;}fos.write(content.getBytes());fos.close();return true;} catch (Exception e){e.printStackTrace();return false;}}/** * 读取文件内容来自手机内存 *  * @param filename 文件名 * @return 文件内容 */public String readFileFromROM(String filename){File file = new File(mContext.getFilesDir(), filename);try{FileInputStream fis = new FileInputStream(file);BufferedReader reader = new BufferedReader(new InputStreamReader(fis));String line;StringBuffer output = new StringBuffer();while ((line = reader.readLine()) != null){output.append(line);}reader.close();return output.toString();} catch (Exception e){e.printStackTrace();return null;}}

===========


-代表0  其他代表1

---    000转换成十进制是0

rw- 110转换成十进制是6


输入命令,跳转到 /data/data/目录

adb shell

#cd /data/data/

输入命令,查看当前目录下文件信息 依次是 文件权限,用户名,用户组,时间,文件名 

# ls -l 


输入命令跳转到包目录

然后输入命令 修改private.txt 的文件权限为666

chmod : change 666 private.txt 


0 0