Android课程---关于数据存储的学习
activity_data1.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="com.hanqi.test5.DataActivity1"
- android:orientation="vertical">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/et_1"
- android:hint="key"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/et_2"
- android:hint="value"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:text="保存"
- android:layout_weight="1"
- android:onClick="bt1_OnClick"/>
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:text="读取"
- android:layout_weight="1"
- android:onClick="bt2_OnClick"/>
- </LinearLayout>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/et_3"
- android:hint="要存储的内容"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/et_4"
- android:hint="从文件中读取的内容"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:text="保存"
- android:layout_weight="1"
- android:onClick="bt3_OnClick"/>
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:text="读取"
- android:layout_weight="1"
- android:onClick="bt4_OnClick"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:text="保存文件"
- android:layout_weight="1"
- android:onClick="bt5_OnClick"/>
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:text="读取文件"
- android:layout_weight="1"
- android:onClick="bt6_OnClick"/>
- </LinearLayout>
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/vipflower"
- android:id="@+id/iv_4"/>
- </LinearLayout>
DataActivity1.java
- package com.hanqi.test5;
- import android.content.SharedPreferences;
- import android.content.res.AssetManager;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.Toast;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.PrintStream;
- public class DataActivity1 extends AppCompatActivity {
- EditText et_1;
- EditText et_2;
- EditText et_3;
- EditText et_4;
- ImageView iv_4;
- SharedPreferences sp;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_data1);
- et_1 = (EditText)findViewById(R.id.et_1);
- et_2 = (EditText)findViewById(R.id.et_2);
- et_3 = (EditText)findViewById(R.id.et_3);
- et_4 = (EditText)findViewById(R.id.et_4);
- iv_4 = (ImageView)findViewById(R.id.iv_4);
- //1.获取sp实例,指定了文件名和操作模式
- sp = getSharedPreferences("mydata",MODE_PRIVATE);
- }
- //操作assets内的文件
- public void bt5_OnClick(View v)
- {
- //1.获取AssetManager管理器
- AssetManager am = getAssets();
- try
- {
- //2.打开文件,获取输入流
- InputStream is = am.open("yuantu.png");
- //3.获取输出流
- FileOutputStream fos = openFileOutput("yuantu.png",MODE_PRIVATE);
- //4.边读边写
- byte[] b = new byte[1024];
- int i =0;
- while ((i = is.read(b))>0)
- {
- fos.write(b,0,i);
- }
- fos.close();
- is.close();
- Toast.makeText(DataActivity1.this, "保存成功", Toast.LENGTH_SHORT).show();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- Toast.makeText(DataActivity1.this, "保存失败", Toast.LENGTH_SHORT).show();
- }
- }
- //从手机内部存储读图片文件
- public void bt6_OnClick(View v)
- {
- //改变ImageView的图片来源,指向手机存储空间
- //1.获取文件存储的绝对路径
- String filepath = getFilesDir().getAbsolutePath();
- //2.组合图片文件的完整路径
- filepath += "/yuantu.png";
- //3.生成位图实例
- Bitmap bm = BitmapFactory.decodeFile(filepath);
- //4.改变ImageView的图片来源
- iv_4.setImageBitmap(bm);
- }
- //文件名
- final String FILENAME = "test.txt";
- public void bt3_OnClick(View v)
- {
- //1.获取要存储的内容
- String content = et_3.getText().toString();
- //2.获取输出流
- try
- {
- FileOutputStream fos_1 = openFileOutput(FILENAME,MODE_APPEND);
- //3.构造PrintStream
- PrintStream pm = new PrintStream(fos_1);
- //4.写入内容
- pm.println(content);
- //5.关闭
- pm.close();
- fos_1.close();
- Toast.makeText(DataActivity1.this, "保存成功", Toast.LENGTH_SHORT).show();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- Toast.makeText(DataActivity1.this, "保存失败", Toast.LENGTH_SHORT).show();
- }
- }
- public void bt4_OnClick(View v)
- {
- try
- {
- //1.获取输入流
- FileInputStream fis = openFileInput(FILENAME);
- //2.定义读取的数组
- byte[] b = new byte[1024];
- //3.读出的数据的长度
- int i =0;
- StringBuilder sb_1 = new StringBuilder();
- while ((i=fis.read(b))>0)
- {
- sb_1.append(new String(b,0,i) );
- }
- fis.close();
- //设置显示读出内容
- et_4.setText(sb_1);
- Toast.makeText(DataActivity1.this, "读取成功", Toast.LENGTH_SHORT).show();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- Toast.makeText(DataActivity1.this, "读取失败", Toast.LENGTH_SHORT).show();
- }
- }
- //保存
- public void bt1_OnClick(View v) {
- //1.获取key和value
- String key = et_1.getText().toString();
- String value = et_2.getText().toString();
- if (key.length() == 0 || value.length() == 0) {
- Toast.makeText(DataActivity1.this, "key或value不能为空", Toast.LENGTH_SHORT).show();
- }
- else {
- //2.取得Editor
- SharedPreferences.Editor editor = sp.edit();
- //3.放入键值对
- editor.putString(key, value);
- //4.提交保存
- boolean b = editor.commit();
- if (b) {
- Toast.makeText(DataActivity1.this, "保存成功", Toast.LENGTH_SHORT).show();
- } else {
- Toast.makeText(DataActivity1.this, "保存失败", Toast.LENGTH_SHORT).show();
- }
- }
- }
- //读取
- public void bt2_OnClick(View v)
- {
- //1.获取要读的key
- String key = et_1.getText().toString();
- //2.读并设置文本框
- et_2.setText(sp.getString(key, "没有发现key"));
- }
- }
数据存储总结之思维导图:
Android课程---关于数据存储的学习的更多相关文章
- Android课程---关于数据存储的学习(2)
手机外部存储的学习 activity_data2.xml <?xml version="1.0" encoding="utf-8"?> <Li ...
- Android课程---关于数据存储的学习(3)之数据库和事务
DataActivity3.java package com.hanqi.test5; import android.content.ContentValues; import android.dat ...
- Android课程---关于数据存储的学习之总结
- Android Learning:数据存储方案归纳与总结
前言 最近在学习<第一行android代码>和<疯狂android讲义>,我的感触是Android应用的本质其实就是数据的处理,包括数据的接收,存储,处理以及显示,我想针对这几 ...
- Android中的数据存储(二):文件存储 2017-05-25 08:16 35人阅读 评论(0) 收藏
文件存储 这是本人(菜鸟)学习android数据存储时接触的有关文件存储的知识以及本人自己写的简单地demo,为初学者学习和使用文件存储提供一些帮助.. 如果有需要查看SharedPreference ...
- 67.Android中的数据存储总结
转载:http://mp.weixin.qq.com/s?__biz=MzIzMjE1Njg4Mw==&mid=2650117688&idx=1&sn=d6c73f9f04d0 ...
- Android中的数据存储
Android中的数据存储主要分为三种基本方法: 1.利用shared preferences存储一些轻量级的键值对数据. 2.传统文件系统. 3.利用SQLite的数据库管理系统. 对SharedP ...
- Android五种数据存储方式
android 五种数据存储 :SharePreferences.SQLite.Contert Provider.File.网络存储 Android系统提供了四种存储数据方式.分别为:SharePre ...
- Android下的数据存储与訪问 --- 以文件的形式
Android下的数据存储与訪问 --- 以文件的形式 1.1 储存文件存放在手机内存中: // *** 储存数据到 /data/data/包名/files/jxn.txt文件里 String dat ...
随机推荐
- 【荐】PDO防 SQL注入攻击 原理分析 以及 使用PDO的注意事项
我们都知道,只要合理正确使用PDO,可以基本上防止SQL注入的产生,本文主要回答以下几个问题: 为什么要使用PDO而不是mysql_connect? 为何PDO能防注入? 使用PDO防注入的时候应该特 ...
- 限制帐号同时两处以上登录-ASP.NET
///登录页面 Hashtable haol = (Hashtable)Application["olTable"]; if (haol == null) { haol = new ...
- python function parameter
Python 2.7.10 (default, Oct 14 2015, 16:09:02) [GCC 5.2.1 20151010] on linux2 Type "copyright&q ...
- poj 1270(toposort)
http://poj.org/problem?id=1270 题意:给一个字符串,然后再给你一些规则,要你把所有的情况都按照字典序进行输出. 思路:很明显这肯定要用到拓扑排序,当然看到discuss里 ...
- tomcat相关问题
动态资源:需要转换成静态资源后再响应给客户端,例如:jsp.servlet,其他语言的动态资源有:asp.php 静态资源:无需转发即可直接响应给客户端,例如:html.css.javascript ...
- 【转】AWK 简明教程
本文转自:http://coolshell.cn/articles/9070.html 有一些网友看了前两天的<Linux下应该知道的技巧>希望我能教教他们用awk和sed,所以,出现了这 ...
- ACM/ICPC 之 网络流-拆点构图(POJ2391)
需要直接到达,因此源点经过三条边后必须要达到汇点,但为了保证网络流的正确性(路径可反悔),因此不可限制层次网络的最高层次为3,最好的方法既是让所有点拆分成两个点,一个点从汇点进入,一个点通向汇点,任意 ...
- Redis 复制、Sentinel的搭建和原理说明
背景: Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-slave的高可用方案时,假如master宕机了,Redis本身(包括它的很多客户端) ...
- redis 密码配置
http://blog.csdn.net/vtopqx/article/details/46833099 http://www.2cto.com/database/201412/365757.html ...
- windows 10 设置
精简应用 邮件和日历: Get-AppxPackage *communi* | Remove-AppxPackage 新闻: Get-AppxPackage *bing* | Remove-AppxP ...