sp,文件以及SDcard存储
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnWrite"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"
android:text="sp存储" />
<Button
android:id="@+id/btnRead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sp读取" />
<Button
android:id="@+id/btnfileWrite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文件存储" />
<Button
android:id="@+id/btnfileRead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文件读取" />
<Button
android:id="@+id/btnFileWriteToSd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SDCard存储" />
<Button
android:id="@+id/btnFileReadToSd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SDCard读取" />
</LinearLayout>
Activity:
public class ShareActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sharepreference);
findViewById(R.id.btnWrite).setOnClickListener(this);
findViewById(R.id.btnRead).setOnClickListener(this);
findViewById(R.id.btnFileWriteToSd).setOnClickListener(this);
findViewById(R.id.btnfileWrite).setOnClickListener(this);
findViewById(R.id.btnfileRead).setOnClickListener(this);
findViewById(R.id.btnFileReadToSd).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnWrite:
SharedPreferences sp = getSharedPreferences("setting",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("name", "张三");
editor.putInt("age", 25);
editor.putInt("weight", 120);
editor.commit();
break;
case R.id.btnRead:
SharedPreferences spread = getSharedPreferences("setting",
Context.MODE_PRIVATE);
String name = spread.getString("name", "null");
int age = spread.getInt("age", 18);
int weight = spread.getInt("weight", 15);
Toast.makeText(this, name + "--" + age + "--" + weight,
Toast.LENGTH_LONG).show();
break;
case R.id.btnfileWrite:
writeFile();
break;
case R.id.btnfileRead:
readFile();
break;
case R.id.btnFileWriteToSd:
writeFilesToSDCard();
break;
case R.id.btnFileReadToSd:
readFilesFromSDCard();
break;
}
}
// 写文件
public void writeFile() {
FileOutputStream os = null;
try {
os = this.openFileOutput("jerei.txt", Context.MODE_PRIVATE);
os.write("姓名:张三".getBytes());
os.write("年龄:25".getBytes());
os.write("体重:100".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
os = null;
}
}
}
//读文件
public void readFile() {
FileInputStream is = null;
StringBuilder sb = new StringBuilder();
try {
is = this.openFileInput("jerei.txt");
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
String tmp = new String(buffer, 0, len);
sb.append(tmp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
}
//存进SD卡
public void writeFilesToSDCard() {
String filePath = null;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 获取SDCard根路径
filePath = Environment.getExternalStorageDirectory().toString();
filePath = filePath + File.separator + "jerei" + File.separator
+ "edu";
File fileParent = new File(filePath);
if (!fileParent.exists()) {
fileParent.mkdirs();
}
FileOutputStream os = null;
try {
os = new FileOutputStream(new File(fileParent, "a.txt"));
os.write("向SDCard中写入文件!!".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
//读取SD卡
public void readFilesFromSDCard(){
FileInputStream is=null;
StringBuilder sb=new StringBuilder();
try {
is=this.openFileInput("a.txt");
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
String tmp = new String(buffer, 0, len);
sb.append(tmp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
}
}
sp,文件以及SDcard存储的更多相关文章
- Android 常用工具类之SPUtil,可以修改默认sp文件的路径
参考: 1. 利用Java反射机制改变SharedPreferences存储路径 Singleton1900 2. Android快速开发系列 10个常用工具类 Hongyang import ...
- Android--数据持久化之内部存储、Sdcard存储
前言 之前一直在讲AndroidUI的内容,但是还没有完结,之后会慢慢补充.今天讲讲其他的,关于数据持久化的内容.对于一个应用程序而言,不可避免的要能够对数据进行存储,Android程序也不例外.而在 ...
- 万能存储工具类SDCard存储 /data/data/存储 assets存储 raw存储
万能存储工具类 SDCard存储 /data/data/存储 assets存储 raw存储 粘贴过去就能够用了 <uses-permission android:name="and ...
- android sp文件一个键值保存多条信息
之前碰到过这样的问题,sp文件只能够append,或者清空.其实一个键值,通过,分割,或者替代可以实现多条信息的存储.下面是一个举例: package com.ctbri.weather.utils; ...
- IOS数据存储之文件沙盒存储
前言: 之前学习了数据存储的NSUserDefaults,归档和解档,对于项目开发中如果要存储一些文件,比如图片,音频,视频等文件的时候就需要用到文件存储了.文件沙盒存储主要存储非机密数据,大的数据. ...
- 高性能文件缓存key-value存储—Redis
1.高性能文件缓存key-value存储-Memcached 2.ASP.NET HttpRuntime.Cache缓存类使用总结 备注:三篇博文结合阅读,简单理解并且使用,如果想深入学习,请多参考文 ...
- 高性能文件缓存key-value存储—Memcached
1.高性能文件缓存key-value存储—Redis 2.ASP.NET HttpRuntime.Cache缓存类使用总结 备注:三篇博文结合阅读,简单理解并且使用,如果想深入学习,请多参考文章中给出 ...
- SDCard存储
当需要访问SD卡上的文件时,需要按照如下步骤进行 *调用Environment.getExternalStorageState()判读手机上是否插入SD卡(返回MEDIA_MOUNTED则表示已经插入 ...
- 背水一战 Windows 10 (90) - 文件系统: 获取 Package 中的文件, 可移动存储中的文件操作, “库”管理
[源码下载] 背水一战 Windows 10 (90) - 文件系统: 获取 Package 中的文件, 可移动存储中的文件操作, “库”管理 作者:webabcd 介绍背水一战 Windows 10 ...
随机推荐
- poj2368 Buttons Nim取石子游戏
链接:http://poj.org/problem?id=2368 和前面差距还是很大啊囧 代码: k,a;main(i){,i=;i<=k/&&k%i;++i);k%i||(a ...
- 备忘DES简单加密与解密
package com.ego.util; import java.io.IOException; import java.security.SecureRandom; import javax.cr ...
- Java中的接口
1.接口概念 <1>接口可以理解一种特殊的类,由全局常量和公共的抽象方法所组成 <2>类是一种具体实现体,而接口定义了某一批类所需要遵守的规范,接口不必关心这些类的内部数据,也 ...
- spring注解 aop
@Resource(name="as") 为空按类型装配 @autowired 按类型 @quafiler (name="xx") 按名称 spring继承关 ...
- C/C++中const的用法 分类: C/C++ 2015-07-05 00:43 85人阅读 评论(0) 收藏
const是C语言的关键字,经C++进行扩充,变得功能强大,用法复杂.const用于定义一个常变量(只读变量),当const与指针,引用,函数等结合起来使用时,情况会变得复杂的多.下面将从五个方面总结 ...
- 使用lua实现一个简单的事件派发器
设计一个简单的事件派发器,个人觉得最重要的一点就是如何保证事件派发过程中,添加或删除同类事件,不影响事件迭代顺序和结果,只要解决这一点,其它都好办. 为了使用pairs遍历函数,重写了pairs(lu ...
- iOS_Swift初识之使用三种回调方式自定义Button
最近在学习Swift ,发现青玉伏案大神早期用OC写的一篇博客--IOS开发之自定义Button(集成三种回调模式) 很适合用来熟悉Swift的回调方式,于是我就用Swift翻版了一下,具体实现原理 ...
- .split()函数使用方法
split说明 split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(list) split语法 str.split('type',num)[n] 整个语法的意思是:以 ...
- 【转】android 属性动画之 ObjectAnimator
原文网址:http://blog.csdn.net/feiduclear_up/article/details/39255083 前面一篇博客讲解了 android 简单动画之 animtion,这里 ...
- DEX 方法超过64K限制和gradle编译OOM问题解决
如果你是一个android开发者,你至少听说过的Dalvik的蛋疼的64K方法限制.概括地说,在一个DEX文件,你可以调用很多的方法,但你只能调用它们最前面的65,536个 ,因为这是在方法调用集合中 ...