activity_data1.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:paddingBottom="@dimen/activity_vertical_margin"
  7. android:paddingLeft="@dimen/activity_horizontal_margin"
  8. android:paddingRight="@dimen/activity_horizontal_margin"
  9. android:paddingTop="@dimen/activity_vertical_margin"
  10. tools:context="com.hanqi.test5.DataActivity1"
  11. android:orientation="vertical">
  12.  
  13. <EditText
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:id="@+id/et_1"
  17. android:hint="key"/>
  18.  
  19. <EditText
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:id="@+id/et_2"
  23. android:hint="value"/>
  24.  
  25. <LinearLayout
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content">
  28.  
  29. <Button
  30. android:layout_width="0dp"
  31. android:layout_height="wrap_content"
  32. android:text="保存"
  33. android:layout_weight="1"
  34. android:onClick="bt1_OnClick"/>
  35.  
  36. <Button
  37. android:layout_width="0dp"
  38. android:layout_height="wrap_content"
  39. android:text="读取"
  40. android:layout_weight="1"
  41. android:onClick="bt2_OnClick"/>
  42. </LinearLayout>
  43.  
  44. <EditText
  45. android:layout_width="match_parent"
  46. android:layout_height="wrap_content"
  47. android:id="@+id/et_3"
  48. android:hint="要存储的内容"/>
  49.  
  50. <EditText
  51. android:layout_width="match_parent"
  52. android:layout_height="wrap_content"
  53. android:id="@+id/et_4"
  54. android:hint="从文件中读取的内容"/>
  55.  
  56. <LinearLayout
  57. android:layout_width="match_parent"
  58. android:layout_height="wrap_content">
  59.  
  60. <Button
  61. android:layout_width="0dp"
  62. android:layout_height="wrap_content"
  63. android:text="保存"
  64. android:layout_weight="1"
  65. android:onClick="bt3_OnClick"/>
  66.  
  67. <Button
  68. android:layout_width="0dp"
  69. android:layout_height="wrap_content"
  70. android:text="读取"
  71. android:layout_weight="1"
  72. android:onClick="bt4_OnClick"/>
  73. </LinearLayout>
  74. <LinearLayout
  75. android:layout_width="match_parent"
  76. android:layout_height="wrap_content">
  77.  
  78. <Button
  79. android:layout_width="0dp"
  80. android:layout_height="wrap_content"
  81. android:text="保存文件"
  82. android:layout_weight="1"
  83. android:onClick="bt5_OnClick"/>
  84.  
  85. <Button
  86. android:layout_width="0dp"
  87. android:layout_height="wrap_content"
  88. android:text="读取文件"
  89. android:layout_weight="1"
  90. android:onClick="bt6_OnClick"/>
  91. </LinearLayout>
  92. <ImageView
  93. android:layout_width="wrap_content"
  94. android:layout_height="wrap_content"
  95. android:src="@drawable/vipflower"
  96. android:id="@+id/iv_4"/>
  97. </LinearLayout>

DataActivity1.java

  1. package com.hanqi.test5;
  2.  
  3. import android.content.SharedPreferences;
  4. import android.content.res.AssetManager;
  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;
  7. import android.os.Bundle;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.view.View;
  10. import android.widget.EditText;
  11. import android.widget.ImageView;
  12. import android.widget.Toast;
  13.  
  14. import java.io.FileInputStream;
  15. import java.io.FileOutputStream;
  16. import java.io.InputStream;
  17. import java.io.PrintStream;
  18.  
  19. public class DataActivity1 extends AppCompatActivity {
  20.  
  21. EditText et_1;
  22. EditText et_2;
  23.  
  24. EditText et_3;
  25. EditText et_4;
  26.  
  27. ImageView iv_4;
  28.  
  29. SharedPreferences sp;
  30.  
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_data1);
  35.  
  36. et_1 = (EditText)findViewById(R.id.et_1);
  37. et_2 = (EditText)findViewById(R.id.et_2);
  38.  
  39. et_3 = (EditText)findViewById(R.id.et_3);
  40. et_4 = (EditText)findViewById(R.id.et_4);
  41.  
  42. iv_4 = (ImageView)findViewById(R.id.iv_4);
  43.  
  44. //1.获取sp实例,指定了文件名和操作模式
  45. sp = getSharedPreferences("mydata",MODE_PRIVATE);
  46.  
  47. }
  48.  
  49. //操作assets内的文件
  50. public void bt5_OnClick(View v)
  51. {
  52. //1.获取AssetManager管理器
  53. AssetManager am = getAssets();
  54.  
  55. try
  56. {
  57. //2.打开文件,获取输入流
  58. InputStream is = am.open("yuantu.png");
  59.  
  60. //3.获取输出流
  61. FileOutputStream fos = openFileOutput("yuantu.png",MODE_PRIVATE);
  62.  
  63. //4.边读边写
  64. byte[] b = new byte[1024];
  65.  
  66. int i =0;
  67.  
  68. while ((i = is.read(b))>0)
  69. {
  70. fos.write(b,0,i);
  71. }
  72. fos.close();
  73. is.close();
  74.  
  75. Toast.makeText(DataActivity1.this, "保存成功", Toast.LENGTH_SHORT).show();
  76.  
  77. }
  78. catch (Exception e)
  79. {
  80. e.printStackTrace();
  81. Toast.makeText(DataActivity1.this, "保存失败", Toast.LENGTH_SHORT).show();
  82. }
  83. }
  84.  
  85. //从手机内部存储读图片文件
  86. public void bt6_OnClick(View v)
  87. {
  88. //改变ImageView的图片来源,指向手机存储空间
  89.  
  90. //1.获取文件存储的绝对路径
  91. String filepath = getFilesDir().getAbsolutePath();
  92.  
  93. //2.组合图片文件的完整路径
  94. filepath += "/yuantu.png";
  95.  
  96. //3.生成位图实例
  97. Bitmap bm = BitmapFactory.decodeFile(filepath);
  98.  
  99. //4.改变ImageView的图片来源
  100. iv_4.setImageBitmap(bm);
  101. }
  102.  
  103. //文件名
  104. final String FILENAME = "test.txt";
  105. public void bt3_OnClick(View v)
  106. {
  107. //1.获取要存储的内容
  108. String content = et_3.getText().toString();
  109. //2.获取输出流
  110. try
  111. {
  112. FileOutputStream fos_1 = openFileOutput(FILENAME,MODE_APPEND);
  113.  
  114. //3.构造PrintStream
  115. PrintStream pm = new PrintStream(fos_1);
  116.  
  117. //4.写入内容
  118. pm.println(content);
  119.  
  120. //5.关闭
  121. pm.close();
  122.  
  123. fos_1.close();
  124.  
  125. Toast.makeText(DataActivity1.this, "保存成功", Toast.LENGTH_SHORT).show();
  126. }
  127. catch (Exception e)
  128. {
  129. e.printStackTrace();
  130.  
  131. Toast.makeText(DataActivity1.this, "保存失败", Toast.LENGTH_SHORT).show();
  132. }
  133. }
  134.  
  135. public void bt4_OnClick(View v)
  136. {
  137. try
  138. {
  139. //1.获取输入流
  140. FileInputStream fis = openFileInput(FILENAME);
  141.  
  142. //2.定义读取的数组
  143. byte[] b = new byte[1024];
  144.  
  145. //3.读出的数据的长度
  146. int i =0;
  147.  
  148. StringBuilder sb_1 = new StringBuilder();
  149.  
  150. while ((i=fis.read(b))>0)
  151. {
  152. sb_1.append(new String(b,0,i) );
  153. }
  154. fis.close();
  155.  
  156. //设置显示读出内容
  157. et_4.setText(sb_1);
  158.  
  159. Toast.makeText(DataActivity1.this, "读取成功", Toast.LENGTH_SHORT).show();
  160. }
  161. catch (Exception e)
  162. {
  163. e.printStackTrace();
  164. Toast.makeText(DataActivity1.this, "读取失败", Toast.LENGTH_SHORT).show();
  165. }
  166. }
  167.  
  168. //保存
  169. public void bt1_OnClick(View v) {
  170. //1.获取key和value
  171. String key = et_1.getText().toString();
  172. String value = et_2.getText().toString();
  173.  
  174. if (key.length() == 0 || value.length() == 0) {
  175. Toast.makeText(DataActivity1.this, "key或value不能为空", Toast.LENGTH_SHORT).show();
  176. }
  177. else {
  178. //2.取得Editor
  179. SharedPreferences.Editor editor = sp.edit();
  180.  
  181. //3.放入键值对
  182. editor.putString(key, value);
  183.  
  184. //4.提交保存
  185. boolean b = editor.commit();
  186.  
  187. if (b) {
  188. Toast.makeText(DataActivity1.this, "保存成功", Toast.LENGTH_SHORT).show();
  189. } else {
  190. Toast.makeText(DataActivity1.this, "保存失败", Toast.LENGTH_SHORT).show();
  191. }
  192. }
  193. }
  194. //读取
  195. public void bt2_OnClick(View v)
  196. {
  197.  
  198. //1.获取要读的key
  199. String key = et_1.getText().toString();
  200.  
  201. //2.读并设置文本框
  202. et_2.setText(sp.getString(key, "没有发现key"));
  203.  
  204. }
  205.  
  206. }

数据存储总结之思维导图:

Android课程---关于数据存储的学习的更多相关文章

  1. Android课程---关于数据存储的学习(2)

    手机外部存储的学习 activity_data2.xml <?xml version="1.0" encoding="utf-8"?> <Li ...

  2. Android课程---关于数据存储的学习(3)之数据库和事务

    DataActivity3.java package com.hanqi.test5; import android.content.ContentValues; import android.dat ...

  3. Android课程---关于数据存储的学习之总结

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

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

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

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

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

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

  7. Android中的数据存储

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

  8. Android五种数据存储方式

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

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

    Android下的数据存储与訪问 --- 以文件的形式 1.1 储存文件存放在手机内存中: // *** 储存数据到 /data/data/包名/files/jxn.txt文件里 String dat ...

随机推荐

  1. 【荐】PDO防 SQL注入攻击 原理分析 以及 使用PDO的注意事项

    我们都知道,只要合理正确使用PDO,可以基本上防止SQL注入的产生,本文主要回答以下几个问题: 为什么要使用PDO而不是mysql_connect? 为何PDO能防注入? 使用PDO防注入的时候应该特 ...

  2. 限制帐号同时两处以上登录-ASP.NET

    ///登录页面 Hashtable haol = (Hashtable)Application["olTable"]; if (haol == null) { haol = new ...

  3. python function parameter

    Python 2.7.10 (default, Oct 14 2015, 16:09:02) [GCC 5.2.1 20151010] on linux2 Type "copyright&q ...

  4. poj 1270(toposort)

    http://poj.org/problem?id=1270 题意:给一个字符串,然后再给你一些规则,要你把所有的情况都按照字典序进行输出. 思路:很明显这肯定要用到拓扑排序,当然看到discuss里 ...

  5. tomcat相关问题

    动态资源:需要转换成静态资源后再响应给客户端,例如:jsp.servlet,其他语言的动态资源有:asp.php 静态资源:无需转发即可直接响应给客户端,例如:html.css.javascript ...

  6. 【转】AWK 简明教程

    本文转自:http://coolshell.cn/articles/9070.html 有一些网友看了前两天的<Linux下应该知道的技巧>希望我能教教他们用awk和sed,所以,出现了这 ...

  7. ACM/ICPC 之 网络流-拆点构图(POJ2391)

    需要直接到达,因此源点经过三条边后必须要达到汇点,但为了保证网络流的正确性(路径可反悔),因此不可限制层次网络的最高层次为3,最好的方法既是让所有点拆分成两个点,一个点从汇点进入,一个点通向汇点,任意 ...

  8. Redis 复制、Sentinel的搭建和原理说明

    背景: Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-slave的高可用方案时,假如master宕机了,Redis本身(包括它的很多客户端) ...

  9. redis 密码配置

    http://blog.csdn.net/vtopqx/article/details/46833099 http://www.2cto.com/database/201412/365757.html ...

  10. windows 10 设置

    精简应用 邮件和日历: Get-AppxPackage *communi* | Remove-AppxPackage 新闻: Get-AppxPackage *bing* | Remove-AppxP ...