layout文件:

 <?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.testapp3.MainActivity"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/tv_1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SP存储"
android:onClick="bt_onClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SP读取"
android:onClick="bt1_onClick"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入……"
android:id="@+id/et_1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="写内部文件"
android:onClick="bt2_onClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读内部文件"
android:onClick="bt3_onClick"/>
</LinearLayout>

java类:

 package com.hanqi.testapp3;

 import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream; public class MainActivity extends AppCompatActivity { EditText et_1;
TextView tv_1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_1 = (EditText)findViewById(R.id.et_1);
tv_1 = (TextView)findViewById(R.id.tv_1);
}
public void bt_onClick(View v) //SharedPreferences存储
{
//1.得到SharedPreferences对象
SharedPreferences sharedPreferences = getSharedPreferences("abc", MODE_APPEND);
//2.得到编辑器
SharedPreferences.Editor editor =sharedPreferences.edit();
//3.使用Editor添加数据
// editor.putString("a","abcdef");
// editor.putLong("long",12345);
editor.remove("a");
//4.提交保存
editor.commit();
Toast.makeText(MainActivity.this, "保存数据成功", Toast.LENGTH_SHORT).show();
}
//读取
public void bt1_onClick(View v)
{
SharedPreferences sp = getSharedPreferences("abc",MODE_PRIVATE);
String str = sp.getString("a", "默认值");
Toast.makeText(MainActivity.this, "key = a"+" value = "+str, Toast.LENGTH_SHORT).show();
}
//写内部文件(手机内部文件存储)
public void bt2_onClick(View v)
{
//从内存里写入文件
//1.得到内部的存储目录
try {
File file = getFilesDir();
String path = file.getAbsolutePath();
Toast.makeText(MainActivity.this, "path = "+path, Toast.LENGTH_SHORT).show();
//2.用输出流写入文件
FileOutputStream fos = openFileOutput("test.txt",MODE_APPEND);
//3.写入文件内容
PrintStream ps = new PrintStream(fos); String str = et_1.getText().toString(); ps.print(str);
// ps.print("测试");
// ps.println("自动换行");
ps.close(); fos.close();
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
} }
public void bt3_onClick(View v)
{
try {
//输入流
FileInputStream fis = openFileInput("test.txt");
//1.定义byte[]
byte[] b = new byte[1024];
int i = 0;//读到的数据长度
String str1 = "";
//2.循环读
while ((i = fis.read(b))>0)
{
String str = new String(b,0,i);
str1 += str;
}
fis.close();
tv_1.setText(str1);
}
catch (Exception e)
{ }
}
}

Android——数据存储(课堂代码整理:SharedPreferences存储和手机内部文件存储)的更多相关文章

  1. Android——手机内部文件存储(作业)

    作业:把用户的注册信息存储到文件里,登录成功后读出并显示出来 activity_practice2的layout文件: <?xml version="1.0" encodin ...

  2. 【Android】14.1 内部文件存储和读取

    分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 内部存储(Internal storage)是指将应用程序建立的私有文件保存在内部存储器(移动经销商卖的那种容量较 ...

  3. android-数据存储之手机内部file存储

    一.基础概要 1.说明: 1>应用程序运行需要一些较大的数据或者图片可保存在手机内部 2>文件类型:任意 3>路径:/data/data/packageName/files/ 4&g ...

  4. Android——FileOutputStream与openFileOutput()的区别分析【第一个用于文件存储,第二个用于SD卡存储】【转】

    本文实例分析了Android编程中FileOutputStream与openFileOutput()的区别.分享给大家供大家参考,具体如下: openFileOutput() 首先给大家介绍使用文件如 ...

  5. 数据交互 ajax代码整理

    请求列表通用 /** **加载对应的试卷套题 ** */ function loadQuestions(){ var businessSubClass = { pageNo:pageNo, pageS ...

  6. Android 模拟登陆 保存密码(信息)到手机中 文件信息读取

    package com.wuyou.login; import java.io.IOException; import java.util.Map; import android.app.Activi ...

  7. Android开发学习——android数据存储

    Android的存储 Android中的数据存储方式及其存储位置 SharedPrefrence存储 1). 位置           /data/data/packageName/shared_pr ...

  8. Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences

    除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...

  9. (转)Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences

    除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...

随机推荐

  1. python走起之第一话

    Python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言 ...

  2. STM32学习笔记(七) ADC模数转换测电平(普通和DMA模式)

    嵌入式系统在微控制领域(温度,湿度,压力检测,四轴飞行器)中占据着重要地位,这些功能的实现是由微处理器cpu(如stm32)和传感器以及控制器共同完成的,而连接他们,使它们能够互相正常交流的正是本小节 ...

  3. 中国Linux源镜像站大全

    原文链接:http://www.centoscn.com/yunwei/news/2012/1227/131.html 一.国内的linux源镜像站点: 1.企业源:阿里云开源镜像站: http:// ...

  4. spark standalone模式单节点启动多个executor

    以前为了在一台机器上启动多个executor都是通过instance多个worker来实现的,因为standalone模式默认在一台worker上启动一个executor,造成了很大的不便利,并且会造 ...

  5. android开发文档工具集(持续更新中...)

     http://www.androiddevtools.cn/ android 产品->交互->视觉->开发->测试各种工具地址下载, 各种文档下载应有尽有,强烈推荐.  ht ...

  6. 0040 Linux 系统管理命令

    1. 主机名称 hostname hostnamectl 2.开机启动 chkconfig systemctl 3.服务管理 service 服务名  start service 服务名  stop ...

  7. 002_kafka_相关术语详细解析

    参考: http://www.cnblogs.com/likehua/p/3999538.html http://kafka.apache.org/documentation.html#getting ...

  8. 51nod 1445 变色DNA(dij)

    题目链接:51nod 1445 变色DNA 看了相关讨论再去用最短路:val[i][j]之间如果是'Y',说明i可以到达j,并且i到达j的代价是i那行 1到j-1 里面'Y'的数量. 最后,求 0到n ...

  9. 安卓使用pull解析器解析XML文件

    学习一下: public class MainActivity extends Activity { List<City> cityList; @Override protected vo ...

  10. jquery中的cookie操作

    使用前在页面中引入下面的代码 /*! * jQuery Cookie Plugin v1.4.1 * https://github.com/carhartl/jquery-cookie * * Cop ...