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. svn ubuntu command(转载)

    转载来自:http://blog.csdn.net/pkueecser/article/details/6830758 将文件checkout到本地目录 svn checkout path(path ...

  2. unittest可能面临的问题以及解决方法

    问题1:用例的执行顺序 当使用unittest.main()时,用例的执行是按照ascall值的顺序来执行的,所以如果使用main()方法来执行用例的话,那么就需要通过命名来限制执行顺序,比如想要先执 ...

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

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

  4. Unity NGUI 2D场景添加按钮

    比如说先添加一个sprite 在sprite加上NGUI的 UI Button 然后重点来了  加上Box Collider 2D(重点:2D 千万不要加 Box Collider) 将Box Col ...

  5. winform app.config文件的动态配置

    获取 获取应用程序exe.config文件中  节点value值 /// <summary> /// 功能: 读取应用程序exe.config文件中 /// appSettings节点下 ...

  6. indent guides 格式化代码(添加竖线)

    点击 Visual Studio 2013 工具—扩展和更新—联机 然后输入indent guides 自动搜索出来这个插件(如图).注:Visual Studio 2010需要自己在网上下载安装. ...

  7. phpstormn 中 xdebug 的详细配置2

    配置PHPStorm 图1:首先配置PHP解释器的路径 图2:File>Settings>PHP>Servers,这里要填写服务器端的相关信息,name填localhost,host ...

  8. NetworkComms V3 之发送UDP广播消息

    NetworkComms网络通信框架序言 NetworkComms通信框架,是一款来自英国的c#语言编写的通信框架,历时6年研发,成熟稳定,性能可靠. NetworkComms v3目前只支持基本的U ...

  9. 为speedphp最新版添加 仿Yii 的简易版 数据验证 支持不同场景,自定义回调

    给个意见或建议吧 扩展一个Model基类 <?php class BaseModel extends Model{ use ValidationRules; public function ru ...

  10. 为什么使用 Bootstrap?

    为什么使用 Bootstrap? 移动设备优先:自 Bootstrap 3 起,框架包含了贯穿于整个库的移动设备优先的样式. 浏览器支持:所有的主流浏览器都支持 Bootstrap.      容易上 ...