Android通过上下文获取常用目录context.openFileOutput();
来源:互联网 发布:techmark数据分析模型 编辑:程序博客网 时间:2024/04/29 00:27
public class UserInfoUtils { // 保存用户名和密码的业务方法 public static boolean saveInfo(Context context, String username, String pwd) { //在原有函数的基础上传一个上下文进来,因为我们要用到上下文,所以谁调用谁传。 try { String result = username + "##" + pwd; /*2 通过传进来的上下文获取FileOutputStream,第一个参数,代表文件名称,注意这里的文件名称不能包括任何的/或者/这种分隔符,只能是文件名 该文件会被保存在/data/data/应用名称/files/infoo.txt MODE_PRIVATE 私有(只能创建它的应用访问) 重复写入时会文件覆盖 MODE_APPEND 私有 重复写入时会在文件的末尾进行追加,而不是覆盖掉原来的文件 * MODE_WORLD_READABLE 公用 可读 * MODE_WORLD_WRITEABLE 公用 可读写 */ FileOutputStream fos = context.openFileOutput("infoo.txt", Context.MODE_PRIVATE); fos.write(result.getBytes()); fos.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } // 读取用户的信息 public static Map<String ,String> readInfo(Context context) { // 通过函数参数方式,传进来一个上下文 try { //1 定义Map Map<String, String> maps = new HashMap<String, String>(); File file = new File("data/data/com.example.a10_login/info.txt"); FileInputStream fis = context.openFileInput("infoo.txt"); BufferedReader bufr = new BufferedReader(new InputStreamReader(fis)); String content = bufr.readLine(); // 读取数据 // 切割字符串 封装到map集合中 String[] splits = content.split("##"); String name = splits[0]; String pwd = splits[1]; // 把name 和 pwd 放入map中 maps.put("name", name); maps.put("pwd", pwd); fis.close(); return maps; } catch (IOException e) { e.printStackTrace(); return null; } }}
2 0
- Android通过上下文获取常用目录context.openFileOutput();
- Android获取上下文Context
- Android JNI获取上下文Context
- Android:根据上下文Context获取Activity
- Android全局获取Context上下文实例
- android BaseFragment获取Context上下文方法
- android context获取目录详解
- android context类提供的openFileOutput()方法
- 获取上下文环境context
- [android]Context 上下文
- Android Context 上下文
- android context上下文
- Android Context 上下文
- android context 上下文
- Android上下文对象Context
- Android Context 上下文
- Android Context 上下文
- Android上下文-Context
- Failed to save the updated manifest to the file "Debug\ex07_1.exe.embed.man
- 第四届 山东省ACM Boring Counting(划分树+二分 主席树待整理)
- 反射_反射概述
- JS调用父页面的DIV,Ipunt等等属性(适用于父页面包含子页面)
- 理解JAVA程序逻辑及面向对象编程思想简单总结(6)
- Android通过上下文获取常用目录context.openFileOutput();
- ambari-agent节点掉线问题总结
- 数据是如何分类的—MySQL数据存储
- 文章标题
- ReentrantLock--synchronized和ReentrantLock区别及使用
- 百度语音集成
- 聚类分析
- bzoj 1030 文本生成器 AC自动机+DP
- jquery表单验证插件:Validform常用的验证规则