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. 利用反射和ResultSetMetaData实现DBUtils的基本功能

    DBUtils大大简化了JDBC的书写,极大的提高了开发效率,和数据库连接池一起,简化了JDBC开发的流程.简易的自定义数据库连接池可以通过装饰者设计模式和动态代理模式得到很简单的实现,那么DBUti ...

  2. Android之alertDialog、ProgressDialog

    一.alertDialog 置顶于所有控件之上的,可以屏蔽其他控件的交互能力.通过AlertDialog.Builder创建一个AlertDialog,并通过setTittle(),setMesseg ...

  3. text属性

    -------------------------------------------------------------------------------- 对p标签进行样式的设置 text-ju ...

  4. OneSQL的docker之旅

      百度盘下载地址: http://pan.baidu.com/s/1v9GWA   OneSQL Docker使用方法:  1. 解压    tar zxvf OneSql-Docker-5.6.2 ...

  5. Recover Binary Search Tree [LeetCode]

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  6. Nginx reopen reload作用及工作过程

    http://www.iigrowing.cn/nginx-reopen-reload-zuo-yong-ji-gong-zuo-guo-cheng.html Nginx reopen reload作 ...

  7. uva 1658(最小费用最大流)

    题意:一个带权有向图,求起点到终点的两条路径权值之和最小,且两条路径没有公共点(除起点,终点): 分析:拆点法,将u拆成u和u',u-u'容量为1,费用为0,这样就能保证每个点只用一次,起点s-s'容 ...

  8. sphinx索引文件进一步说明——最好是结合lucene一起看,直觉告诉我二者本质无异

    摘自:http://blog.csdn.net/cangyingzhijia/article/details/8592441 Sphinx使用的文件包括 "sph", " ...

  9. log4net.NoSql +ElasticSearch 实现日志记录

    前言: 前两天在查找如何扩展log4net的日志格式时找到一个开源项目Log4net.NoSql,它通过扩展Appender实现了把日志输出到ElasticSearch里面.顺藤摸瓜,发现涉及的项目还 ...

  10. ionic 安装本地插件极光推送

    问题:按照官方文档的步骤 假如把插件保存到了D:\push\jpush,当执行到 cordova plugin add D:\push\jpush 的时候,ionic 不是从本地目录安装,而是从reg ...