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存储的更多相关文章

  1. Android 常用工具类之SPUtil,可以修改默认sp文件的路径

    参考: 1. 利用Java反射机制改变SharedPreferences存储路径    Singleton1900 2. Android快速开发系列 10个常用工具类 Hongyang import ...

  2. Android--数据持久化之内部存储、Sdcard存储

    前言 之前一直在讲AndroidUI的内容,但是还没有完结,之后会慢慢补充.今天讲讲其他的,关于数据持久化的内容.对于一个应用程序而言,不可避免的要能够对数据进行存储,Android程序也不例外.而在 ...

  3. 万能存储工具类SDCard存储 /data/data/存储 assets存储 raw存储

    万能存储工具类 SDCard存储  /data/data/存储  assets存储 raw存储 粘贴过去就能够用了 <uses-permission android:name="and ...

  4. android sp文件一个键值保存多条信息

    之前碰到过这样的问题,sp文件只能够append,或者清空.其实一个键值,通过,分割,或者替代可以实现多条信息的存储.下面是一个举例: package com.ctbri.weather.utils; ...

  5. IOS数据存储之文件沙盒存储

    前言: 之前学习了数据存储的NSUserDefaults,归档和解档,对于项目开发中如果要存储一些文件,比如图片,音频,视频等文件的时候就需要用到文件存储了.文件沙盒存储主要存储非机密数据,大的数据. ...

  6. 高性能文件缓存key-value存储—Redis

    1.高性能文件缓存key-value存储-Memcached 2.ASP.NET HttpRuntime.Cache缓存类使用总结 备注:三篇博文结合阅读,简单理解并且使用,如果想深入学习,请多参考文 ...

  7. 高性能文件缓存key-value存储—Memcached

    1.高性能文件缓存key-value存储—Redis 2.ASP.NET HttpRuntime.Cache缓存类使用总结 备注:三篇博文结合阅读,简单理解并且使用,如果想深入学习,请多参考文章中给出 ...

  8. SDCard存储

    当需要访问SD卡上的文件时,需要按照如下步骤进行 *调用Environment.getExternalStorageState()判读手机上是否插入SD卡(返回MEDIA_MOUNTED则表示已经插入 ...

  9. 背水一战 Windows 10 (90) - 文件系统: 获取 Package 中的文件, 可移动存储中的文件操作, “库”管理

    [源码下载] 背水一战 Windows 10 (90) - 文件系统: 获取 Package 中的文件, 可移动存储中的文件操作, “库”管理 作者:webabcd 介绍背水一战 Windows 10 ...

随机推荐

  1. nodejs--模块

    在客户端可以将所有的javascript代码分割成几个JS文件,然后在浏览器中将这些JS文件合并.但是在nodejs中是通过以模块为单位来划分所有功能的.每一个模块为一个JS文件,每一个模块中定义的全 ...

  2. Kindle Unlimited上的技术书籍

            直达链接:Kindle Unlimited         前不久,亚马逊在中国也推出了电子书包月服务.消息不灵通的我过了好久才看到这个消息,随后第一时间上官网查看具体情况.      ...

  3. [NOIP2015] 斗地主(搜索)

    题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...

  4. Debian普通用户获取root权限|sudo的安装与配置

    Debian系统的普通用户需要安装软件时,往往会收到“Permission denied”的提示,这时候需要root权限.那么如何在不登陆超级管理员账户的前提下拥有root权限呢?对于大多数Linux ...

  5. C#参数化执行SQL语句,防止漏洞攻击本文以MySql为例【20151108非查询操作】

    为什么要参数化执行SQL语句呢? 一个作用就是可以防止用户注入漏洞. 简单举个列子吧. 比如账号密码登入,如果不用参数, 写的简单点吧,就写从数据库查找到id和pw与用户输入一样的数据吧 sql:se ...

  6. 取得系统属性和Java环境

    代码如下: import java.util.Enumeration; import java.util.Iterator; import java.util.Map; import java.uti ...

  7. 嵌套循环中break、continue的用法

    在循环语句中经常会用到break.continue语句,基本用法如下: 一.break语句为退出当前循环,在嵌套循环中结果如下: var num=0; for(var i=0;i<5;i++){ ...

  8. python统计元素重复次数

    python统计元素重复次数 # !/usr/bin/python3.4 # -*- coding: utf-8 -*- from collections import Counter arr = [ ...

  9. MyBatis代码自动生成(利用命令)

    这几天在学习springmvc,需要用到mybatis,所以研究了一下mybatis自动代码生成,当然也可以手动敲,但是那样效率非常的慢,并且出错率也是很高的,利用MyBatis生成器自动生成实体类. ...

  10. Ubuntu16.04 安装配置Caffe

    Caffe已经是第三次安装配置了,为什么是第三次呢?因为我实在是低估了深度学习对于硬件的要求.第一次我在自己笔记本上配置的单核,CPU only ...  结果是,样例数据跑了4小时,这还怎么玩?第二 ...