1. SharedPreference

SharedPreference可以很容易的保存key-value对,通常用于保存配置信息。
保存的步骤

1. 获得SharedPreferences对象  (最后一个参数指定了文件的建立模式,设置文件属性)
  SharedPreferences mySharedPreference = getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
2. 获得SharedPreferences.Editor对象
  SharedPreferences.Editor editor = mySharedPreference.edit();
3. 保存组件中的值
  editor.putString("name",edtname.getText().toString());
  editor.putInt("age",Integer.valueOf(edtage.getText().toString()));
4. 提交保存的结果
  editor.commit();

demo界面如下,实现的功能:点击保存信息将姓名、年龄保存。重启demo后,点击显示信息,将之前保存的内容显示到界面。

MainActivity.java

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends ActionBarActivity {
//SharedPreference可以很容易的保存key-value对,因此通常用于保存配置信息
//SharedPreference会将key-value对保存在survey.xml文件中
private final String PREFERENCE_NAME = "survey";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText edtname = (EditText)findViewById(R.id.edt1);
final EditText edtage = (EditText)findViewById(R.id.edt2);
Button btn1 = (Button)findViewById(R.id.btn1);
Button btn2 = (Button)findViewById(R.id.btn2);
btn1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//1. 获得SharedPreferences对象 (最后一个参数指定了文件的建立模式,设置文件属性)
SharedPreferences mySharedPreference = getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
//2. 获得SharedPreferences.Editor对象
SharedPreferences.Editor editor = mySharedPreference.edit();
//3. 保存组件中的值
editor.putString("name",edtname.getText().toString());
editor.putInt("age",Integer.valueOf(edtage.getText().toString()));
//4. 提交保存的结果
editor.commit(); Toast.makeText(MainActivity.this, "信息已保存", Toast.LENGTH_SHORT).show();
}
}); btn2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//获得SharedPreferences对象
SharedPreferences mySharedPreference = getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
//获得保存的值
String name = mySharedPreference.getString("name", "");
int age = mySharedPreference.getInt("age", 0);
Toast.makeText(MainActivity.this, "姓名:"+name+" 年龄:"+age, Toast.LENGTH_SHORT).show(); edtname.setText(name);
edtage.setText(String.valueOf(age));
}
});
} @Override
protected void onStop(){
super.onStop();
}
}

activity_main.xml

<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"
android:orientation="vertical"
tools:context="com.example.andtest008sharedpreferences.MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名" />
<EditText
android:id="@+id/edt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint = "请输入姓名"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄" />
<EditText
android:id="@+id/edt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint = "请输入年龄"
/>
<Button
android:id = "@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="保存信息"
/>
<Button
android:id = "@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示信息"
/>
</LinearLayout>

2.文件IO

使用的工具OutputStream/InputStream

向file.txt写入内容
OutputStream outer = openFileOutput("file.txt",Activity.MODE_PRIVATE);

读取file.txt的内容
InputStream inner = openFileInput("file.txt");

package com.evor.test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView; public class MainActivity extends ActionBarActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); try {
//向文件写入内容
OutputStream outer = openFileOutput("file.txt",Activity.MODE_PRIVATE);
String str = "Test:这是一行测试文字!!";
outer.write(str.getBytes("utf-8"));
outer.close(); //读取文件的内容,并显示在textView
InputStream inner = openFileInput("file.txt");
byte[] buffer = new byte[100];
int bytecount = inner.read(buffer);
String str2 = new String(buffer,0,bytecount,"utf-8");
TextView view = (TextView)findViewById(R.id.showtxt);
view.setText(str2);
inner.close(); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }
}

activity_main.xml

<RelativeLayout 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.evor.test.MainActivity" > <TextView
android:id="@+id/showtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> </RelativeLayout>

Android学习笔记--存储方案(SharedPreference、文件IO)的更多相关文章

  1. Android学习笔记之AndroidManifest.xml文件解析(转)

    //自已备注: <?xml version="1.0" encoding="utf-8"?>//说明了版本号,字符集 <manifest xm ...

  2. Android学习笔记之AndroidManifest.xml文件解析

    一.关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activiti ...

  3. Android学习笔记_43_网络通信之文件断点上传

    1.建立服务端,用于接收上传的文件.这里使用Socket,文件可能会比较大.采用多线程编程,防止并发. package com.socket.service; import java.io.File; ...

  4. Android学习笔记_15_网络通信之文件断点下载

    一.断点下载原理: 使用多线程下载文件可以更快完成文件的下载,多线程下载文件之所以快,是因为其抢占的服务器资源多.如:假设服务器同时最多服务100个用户,在服务器中一条线程对应一个用户,100条线程在 ...

  5. Android学习笔记之AndroidManifest.xml文件解析(详解)

    一.关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activiti ...

  6. Android学习笔记(9):使用XML文件和Java代码控制UI界面

    Android推荐使用XML文件设置UI界面.然后用Java代码控制逻辑部分,这体现了MVC思想. MVC全名是Model View Controller.是模型(model)-视图(view)-控制 ...

  7. Android学习笔记(1)----播放音乐文件

    原文地址:http://www.cnblogs.com/wynet/p/5526905.html 这里介绍两种播放资源文件的方法: 第一种. assets类资源放在工程根目录的assets子目录下,它 ...

  8. Android学习笔记36:使用SQLite方式存储数据

    在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...

  9. android学习笔记36——使用原始XML文件

    XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...

随机推荐

  1. mobile javascript

    http://www.cnblogs.com/zhepama/archive/2013/06/01/3112023.html http://www.csdn.net/article/2014-01-0 ...

  2. 我想要个pc和手机共有的客户端,就像百度云(iBarn网盘好用)

    https://github.com/zhimengzhe/iBarn iBarn网盘是一个基于PHP开发的先进的云存储系统,提供文件的网络备份,同步和分享服务:支持断点续传,秒传等功能:可选择文件下 ...

  3. 排序功能实现 jQuery实现排序 上移 下移

    效果 思路, 跟相邻元素,互换sort. 前提是每一个元素都有自己的sort值,不为零. <tr id="{sh:$vo.id}"> <td> <sp ...

  4. jsonp实现跨域资源访问

    平时项目中处理ajax跨域资源请求时,例如www.example2.com上的某个页面要请求www.example1.com的数据,我们使用得较多的是jsonp方式.jsonp通过JavaScript ...

  5. Android读取Assert文件夹下txt文本并变为String的方法

    使用场景,在assert文件夹下 有些文本文件,我们需要通过工具类读取出来,然后放到String字符串中,我们该如何操作呢: 直接上代码: 使用方法: MyActivity.readAssertRes ...

  6. C#的checked和unchecked

    C#的 checked关键字用于对整型算术运算和转换显式启用溢出检查. 简单点说,我们在进行数值计算时,运算结果可能会超出该类型能表达的数值范围,因而结果溢出.而这个溢出如果是含有变量的表达式的话,编 ...

  7. java中的“包”与C#中的“命名空间

    原文地址:http://www.cnblogs.com/lidabo/archive/2012/12/15/2819865.html Package vs. Namespace 我们知道,重用性(re ...

  8. XAMPP的MYSQL无法启动

    昨天用各种方式试验MYSQL的数据库备份与恢复操作,恢复过程中,MYSQL就无法启动了. 提示如下: 22:59:43 [mysql] Attempting to start MySQL app... ...

  9. github atom创建自己的语法高亮

    使用atom一段时间了,有些插件还不是很成熟.比如项目中使用protobuf,早就有人写了语法高亮(https://github.com/podgib/atom-protobuf),但是效果不是很好. ...

  10. SpringMVC+highstock实现曲线报表

    最近项目要做曲线报表,之前用的是生成图片然后传到前端,感觉不是很好,在网上找到资料说highstock就可以而且还可以做类似股票的那种实时的曲线,研究的一段时间把项目问题解决了做个总结: 首先把hig ...