Android下的数据存储与訪问 --- 以文件的形式

	1.1 储存文件存放在手机内存中:

		// *** 储存数据到 /data/data/包名/files/jxn.txt文件里
String data = "test"; // /data/data/包名/files
File filesDir = context.getFilesDir(); File file = new File(filesDir, "jxn.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write(data.getBytes());
fos.flush();
fos.close(); // *** 从 /data/data/包名/files/jxn.txt文件里读取数据
String data = "test";
File filesDir = context.getFilesDir();
File file = new File(filesDir, "jxn.txt");
FileInputStream fis = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String text = reader.readLine();
reader.close(); 补充:
1,在Android上下文中openFileOutput()方法能够把数据写到/data/data/包名/files文件夹下
2。在Android上下文中openFileInput()方法能够读取/data/data/包名/files文件夹下的文件
3,详细的实现过程与在J2SE环境中保存数据到文件里是一样的。 eg:
// openFileOutput()方法的第一參数用于指定文件名,不能包括路径分隔符“/” ,假设文件不存在。Android会自己主动创建
// 存放路径:/data/data/包名/files/jxn.txt
FileOutputStream outStream = context.openFileOutput("jxn.txt", Context.MODE_PRIVATE); SharedPreferences
1,Android提供了一个SharedPreferences类。它是一个轻量级的存储类。特别适合用于保存软件配置參数。
2。使用SharedPreferences保存数据,其最后是用xml文件存放数据,文件存放在/data/data/包名/shared_prefs文件夹下
3,context.getSharedPreferences(name,mode)方法的第一个參数用于指定该文件的名称,名称不用带后缀,Android会自己主动地加上.xml后缀。 // *** 储存数据到 /data/data/包名/shared_prefs/jxn.xml文件里 // /data/data/包名/shared_prefs/jxn.xml
SharedPreferences sp = context.getSharedPreferences("jxn", Context.MODE_PRIVATE); // 获得一个Editor对象
Editor edit = sp.edit(); // 存数据
edit.putString("number", number);
edit.putString("password", password); // 提交, 数据就保存到文件里了
edit.commit(); // *** 从 /data/data/包名/shared_prefs/jxn.xml文件里读取数据 SharedPreferences sp = context.getSharedPreferences("jxn", Context.MODE_PRIVATE); String number = sp.getString("number", null);
String password = sp.getString("password", null); 1.2 储存文件存放在SD卡中: // *** 储存数据到 /mnt/sdcard/jxn.txt文件里 // 推断当前的手机是否有sd卡;假设有SD卡,而且能够读写。则方法返回Environment.MEDIA_MOUNTED
String state = Environment.getExternalStorageState();
if(!Environment.MEDIA_MOUNTED.equals(state)) {
return false;
} String data = "test"; // 一般为 /mnt/sdcard 可是不同手机的sd卡的文件夹可能不同
File sdCardFile = Environment.getExternalStorageDirectory(); File file = new File(sdCardFile, "jxn.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write(data.getBytes());
fos.flush();
fos.close(); // *** 从 /mnt/sdcard/jxn.txt文件里读取数据 // 推断当前的手机是否有sd卡
String state = Environment.getExternalStorageState();
if(!Environment.MEDIA_MOUNTED.equals(state)) {
return null;
} File sdCardFile = Environment.getExternalStorageDirectory();
File file = new File(sdCardFile, "jxn.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String text = br.readLine();
br.close(); <!-- 设置写入和读取sd卡的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Android下的数据存储与訪问 --- 以文件的形式的更多相关文章

  1. Android中的数据存储(二):文件存储 2017-05-25 08:16 35人阅读 评论(0) 收藏

    文件存储 这是本人(菜鸟)学习android数据存储时接触的有关文件存储的知识以及本人自己写的简单地demo,为初学者学习和使用文件存储提供一些帮助.. 如果有需要查看SharedPreference ...

  2. android下的数据存储

    android下数据存储的几种方式:(简单讨论) 1.文件 举例:登陆时“记住密码” 因为是基于Linux系统,直接建文件,文件会出现在项目工程:而手机登陆时,应该把文件放在手机里,通常数据放在dat ...

  3. Android下的数据存储与访问、权限

    弹出吐司 在onCreate中可以先获取控件对象 /data/data/程序的包名/          在这个目录下面进行文件的读写可能因为包名的改变而变得不可靠. this可以是Activity,也 ...

  4. Android网络编程之使用HTTP訪问网络资源

    使用HTTP訪问网络资源 前面介绍了 URLConnection己经能够很方便地与指定网站交换信息,URLConnection另一个子类:HttpURLConnection,HttpURLConnec ...

  5. 67.Android中的数据存储总结

    转载:http://mp.weixin.qq.com/s?__biz=MzIzMjE1Njg4Mw==&mid=2650117688&idx=1&sn=d6c73f9f04d0 ...

  6. Android Learning:数据存储方案归纳与总结

    前言 最近在学习<第一行android代码>和<疯狂android讲义>,我的感触是Android应用的本质其实就是数据的处理,包括数据的接收,存储,处理以及显示,我想针对这几 ...

  7. Android中的数据存储

    Android中的数据存储主要分为三种基本方法: 1.利用shared preferences存储一些轻量级的键值对数据. 2.传统文件系统. 3.利用SQLite的数据库管理系统. 对SharedP ...

  8. Android五种数据存储方式

    android 五种数据存储 :SharePreferences.SQLite.Contert Provider.File.网络存储 Android系统提供了四种存储数据方式.分别为:SharePre ...

  9. Android——几种数据存储应用浅谈

    (1)android中的数据存储主要有五种方式: 第一种.sharedPreferences存储数据, 适用范围:保存少量的数据,且这些数据的格式非常简单:字符串型.基本类型的值.比如应用程序的各种配 ...

随机推荐

  1. solr params.json

    The Request Parameters API allows creating parameter sets that can override or take the place of par ...

  2. Selenium2+python自动化73-定位的坑:class属性有空格【转载】

    前言 有些class属性中间有空格,如果直接复制过来定位是会报错的InvalidSelectorException: Message: The given selector u-label f-dn ...

  3. django学习随笔:ManagementUtility

    ManagementUtility类,位于django.core.management目录下的__init__.py文件. 这个类,在其init中: def __init__(self, argv=N ...

  4. rest_frameword学前准备

    CBV CBV(class base views) 就是在视图里使用类处理请求. Python是一个面向对象的编程语言,如果只用函数来开发,有很多面向对象的优点就错失了(继承.封装.多态).所以Dja ...

  5. 转载 Ofbiz 入门教程

    1.Ofbiz 介绍: Ofbiz(http://www.ofbiz.org) 是 Open Source 的商务软件系统,充分利用了各优秀的的Open Source 项目,像 Tomcat, Ant ...

  6. 使用python抓取并分析数据—链家网(requests+BeautifulSoup)(转)

    本篇文章是使用python抓取数据的第一篇,使用requests+BeautifulSoup的方法对页面进行抓取和数据提取.通过使用requests库对链家网二手房列表页进行抓取,通过Beautifu ...

  7. pycurl mac 安装报错Curl is configured to use SSL,

    1.使用安装第三方插件的方式安装pycurl:pip3 install pycurl 报错提示如下: Curl is configured to use SSL, but we have not be ...

  8. scrapy模拟请求头

    import random USER_AGENT_LIST=[ 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, lik ...

  9. 洛谷 P1824 进击的奶牛【二分答案/类似青蛙过河】

    题目描述 Farmer John建造了一个有N(2<=N<=100,000)个隔间的牛棚,这些隔间分布在一条直线上,坐标是x1,...,xN (0<=xi<=1,000,000 ...

  10. HDU 1015 Safecracker【数值型DFS】

    Safecracker Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...