Android 中保存数据到文件中
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android 中保存数据到文件中,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2745字,纯文字阅读大概需要4分钟。
内容图文
![Android 中保存数据到文件中](/upload/InfoBanner/zyjiaocheng/1226/2ab4caf99b9a4cf0a7a2772ab1ab960a.jpg)
1、在安卓开发中,会遇到保存数据到手机中以及从手机中获取数据的情况
/** * 把数据存放到手机内存中 * * @param number * @param password * @return */ public static boolean saveUserInfo(Context context, String number, String password) { try { // getCacheDir()方法用于获取/data/data/<package name>/cache目录 缓存数据 // getFilesDir()方法用于获取/data/data/<package name>/files目录 // 定义路径 // String path = "/data/data/com.example.qqlogin/qqlogin.txt"; File filesDir = context.getFilesDir(); // 动态获得路径 File file = new File(filesDir, "qqlogin.txt"); // 输出流,把数据输出到文件中 FileOutputStream fos = new FileOutputStream(file); // 要写入的数据 String data = number + "##" + password; // 写入字节流 fos.write(data.getBytes()); // 清空缓存 fos.flush(); // 关闭流 fos.close(); returntrue; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } returnfalse; } /** * 从文件中读取数据,并返回出去 * * @return*/publicstatic Map<String, String> getUserInfo(Context context) { // String path = "/data/data/com.example.qqlogin/qqlogin.txt"; // 动态获取文件名 File filesDir = context.getFilesDir(); // 动态获得路径 File f = new File(filesDir, "qqlogin.txt"); try { // 从文件中读取流 // FileInputStream fis = new FileInputStream(path); FileInputStream fis = new FileInputStream(f); // 把字节流 转换为 字符串流 BufferedReader reader = new BufferedReader(new InputStreamReader( fis)); String text = reader.readLine(); if (!TextUtils.isEmpty(text)) { String[] spilt = text.split("##"); Map<String, String> userInfoMap = new HashMap<String, String>(); userInfoMap.put("number", spilt[0]); userInfoMap.put("password", spilt[1]); return userInfoMap; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } returnnull; }
2、通过SharedPreferences 向手机中写入数据
/** * 把数据存放到手机内存中 * * @param number * @param password * @return */ public static boolean saveUserInfo(Context context, String number, String password) { try { // 文件保存在 /data/data/包名/shared_prefs/itheima28 SharedPreferences sp = context.getSharedPreferences("aa", context.MODE_PRIVATE); // 获得一个编辑对象 Editor ed = sp.edit(); // 存放数据 ed.putString("number", number); ed.putString("password", password); // 提交数据 ed.commit(); returntrue; } catch (Exception e) { } returnfalse; } /** * 从文件中读取数据,并返回出去 * * @return*/publicstatic Map<String, String> getUserInfo(Context context) { try { // 文件保存在 /data/data/包名/shared_prefs/itheima28 SharedPreferences sp = context.getSharedPreferences("aa",context.MODE_PRIVATE); String number=sp.getString("number", null); String password=sp.getString("password", null); Map<String,String> userInfoMap=new HashMap<String, String>(); userInfoMap.put("number", number); userInfoMap.put("password",password); return userInfoMap; } catch(Exception e) { } returnnull; }
原文:http://www.cnblogs.com/luoyangcn/p/4694605.html
内容总结
以上是互联网集市为您收集整理的Android 中保存数据到文件中全部内容,希望文章能够帮你解决Android 中保存数据到文件中所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。