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. Delphi TTable 组件

    TTable 是 TDataSet 的派生类,它是基于 BDE 数据库引擎的数据集组件,也是一个较简单的数据组件,可以直接从数据库中获取数据表的数据,只需设置连接的数据库属性(Database) 和所 ...

  2. Win7精简成功后的总结

    vsax 发表于 2014-7-20 20:59:43  https://www.itsk.com/forum.php?mod=viewthread&tid=333816&highli ...

  3. MVP 个人理解2

    根据我的理解,画了个图 这次又看了下 较复杂点的例子. 往往一个项目有多个部份,我们可以按功能分成几个activity, 每个activity有自己的view和数据model,因此也有自己的逻辑 pr ...

  4. ios基础篇(十二)——UINavgationController的使用(三)ToolBar

    UIToolBar存在于UINavigationController导航栏控制器中,而且默认被隐藏:设置UINavigationController的toolbarHidden属性可显示UIToolB ...

  5. iOS开发拓展篇—xib中关于拖拽手势的潜在错误

    iOS开发拓展篇—xib中关于拖拽手势的潜在错误 一.错误说明 自定义一个用来封装工具条的类 搭建xib,并添加一个拖拽的手势. 主控制器的代码:加载工具条 封装工具条以及手势拖拽的监听事件 此时运行 ...

  6. iOS 通讯录操作

    转载至:http://superuna.blog.51cto.com/4192682/982938 //新增联系人 -(void)AddPeople {         //取得本地通信录名柄     ...

  7. pycharm的快捷方式

    PyCharm3.0默认快捷键(翻译的)1.编辑(Editing)Ctrl + Space 基本的代码完成(类.方法.属性)Ctrl + Alt + Space 快速导入任意类Ctrl + Shift ...

  8. PHP 数组

    // 这里用数字来作为索引 $myArray = array(2012, 'blue', 5, 'BMW'); // 这个用关键字作为索引 $myAssocArray = array('year' = ...

  9. iOS流量监控

    http://code4app.com/snippets/one/iOS%E6%B5%81%E9%87%8F%E7%9B%91%E6%8E%A7/5020ba7a6803fae325000000 1. ...

  10. QuartZ.net 常用配置说明

    配置文件说明 app.config中的quartz部分 <quartz> <!-- configure Thread Pool--> <addkey="quar ...