Android开发中免不了数据本地的存储,今天我们来说一说怎样利用IO流来进行数据存储。

这里我们通过模拟一个QQ登陆界面的小demo来实际操作IO流。

功能描写叙述:点击button能够保存用户输入的username和password,当点击记住password时。亦能在应用第二次打开时,回显username和password

1.这里布局文件的代码就不贴了,看效果图

2.MainActivity.java

package com.example.viewswitchtest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List; import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity { private EditText editAccount;
private EditText editPwd;
private Button btnLogin;
private CheckBox checkbox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 账号edit
editAccount = (EditText) findViewById(R.id.edit_account);
// passwordedit
editPwd = (EditText) findViewById(R.id.edit_pwd);
// 登陆button
btnLogin = (Button) findViewById(R.id.btn_login);
// 记住账号和passwordcheckbox
checkbox = (CheckBox) findViewById(R.id.checkbox); // 回显账号和password
readAccountAndPwd(); btnLogin.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) { if (checkbox.isChecked()) { String account = editAccount.getText().toString();
String pwd = editPwd.getText().toString(); // android为了防止开发者手写路径easy出现错误,装门提供了两个API
// getFilesDir() --> 相应的路径:data/data/项目包名/files
// getCacheDir() --> 相应的路径:data/data/项目包名/cache
File file = new File(getFilesDir(), "info.txt"); try {
FileOutputStream fos = new FileOutputStream(file);
fos.write((account + "-" + pwd).getBytes()); // 保存格式为"account-password",方便切割
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Toast.makeText(MainActivity.this, "login success", Toast.LENGTH_LONG).show();
}
});
} /**
* 回显username和password
*/
private void readAccountAndPwd() { File file = new File(getFilesDir(), "info.txt"); if (file.exists()) {
try {
FileInputStream fis = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String text = reader.readLine();
String[] infos = text.split("-"); // 回显信息
editAccount.setText(infos[0]);
editPwd.setText(infos[1]); fis.close();
reader.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

3.输入账号和password,点击登陆button,我们能够在DDMS中的file explore中看到保存的txt格式文件:

4.导出文件,通过文本编辑器打开验证,账号password是否正确:

能够看到账号和password被正确的保存了。且格式正确。

5.关闭应用。再次打开

能够看到,数据能够被正确的回显到输入框中。:-D。。!

Android数据存储之IO的更多相关文章

  1. 第8章 Android数据存储与IO——File存储

    openFileOutput/openFileInput 这是android自带的两种解决方案.

  2. android学习笔记45——android的数据存储和IO

    android的数据存储和IO SharedPreferences与Editor简介 SharedPreferences保存的数据主要是类似于配置信息格式的数据,因此其保存的数据主要是简单的类型的ke ...

  3. Android - 数据存储 -存储文件

    Android使用的文件系统和其他平台的基本磁盘的文件系统很相似.这里将要介绍如何使用File API在Android文件系统中读写文件. File对象适合按顺序读写大量的数据.例如,适合图片文件或者 ...

  4. 【Android开发日记】之入门篇(七)——Android数据存储(上)

    在讲解Android的数据源组件——ContentProvider之前我觉得很有必要先弄清楚Android的数据结构. 数据和程序是应用构成的两个核心要素,数据存储永远是应用开发中最重要的主题之一,也 ...

  5. 【Android开发日记】之入门篇(八)——Android数据存储(下)

    废话不多说了,紧接着来讲数据库的操作吧.Come On! 提到数据存储问题,数据库是不得不提的.数据库是用来存储关系型数据的不二利器.Android为开发者提供了强大的数据库支持,可以用来轻松地构造基 ...

  6. Android数据存储之SQLCipher数据库加密

    前言: 最近研究了Android Sqlite数据库(文章地址:Android数据存储之Sqlite的介绍及使用)以及ContentProvider程序间数据共享(Android探索之ContentP ...

  7. Android数据存储之GreenDao 3.0 详解

    前言: 今天一大早收到GreenDao 3.0 正式发布的消息,自从2014年接触GreenDao至今,项目中一直使用GreenDao框架处理数据库操作,本人使用数据库路线 Sqlite----> ...

  8. Android数据存储方式--SharedPreferences

    Android数据存储方式有如下四种:SharedPreferences.存储到文件.SQLite数据库.内容提供者(Content provider).存储到网络服务器. 本文主要介绍一下Share ...

  9. 数据存储与IO(一)

    应用程序沙盒简介:iOS应用程序只能在系统为它分配的文件区域内读写文件,这个区域就是此应用程序的沙盒,Application目录下的GUID文件夹就是沙盒,这个文件夹是系统随机命名的.程序所有的非代码 ...

随机推荐

  1. Pro ASP.NET Core MVC 第6版翻译 目录页

    Pro ASP.NET Core MVC 第6版 目录 第一部分 第一章 ASP.NET Core MVC 的前世今生 第二章 第一个MVC应用程序(上) 第二章 第一个MVC应用程序(下) 第三章 ...

  2. PSP辅助软件开发计划

    PSP辅助软件开发计划 作者: 日期:2013年11月14号 1开发目的 鉴于软件开发过程中,程序员往往无法在规定时间内完成任务,而且无法给出拖延的时间从而造成项目进度计划不准确.开发此软件帮助程序员 ...

  3. Windows下压缩成tar.gz格式

    tar.gz 是linux和unix下面比较常用的格式,几个命令就可以把文件压缩打包成tar.gz格式,然而这种格式在windows并不多见,WinRAR.WinZip等主流压缩工具可以释放解开,却不 ...

  4. 【C++】智能指针简述(四):shared_ptr

    在开始本文内容之前,我们再来总结一下,前文内容: 1.智能指针采用RAII机制,在构造对象时进行资源的初始化,析构对象时进行资源的清理及汕尾. 2.auto_ptr防止拷贝后析构释放同一块内存,采用& ...

  5. Navicat Premium 12 破解方法

    基本安装下一步下一步,破解方法参考:地址

  6. 北大ACM(POJ1020-Anniversary Cake)

    Question:http://poj.org/problem?id=1020 问题点:DFS. Memory: 260K Time: 47MS Language: C++ Result: Accep ...

  7. 网站卡测试用 PageSpeed Insights

    这个是google测试网页的;https://developers.google.com/speed/pagespeed/insights/ PageSpeed Insights 简介 PageSpe ...

  8. spring用来干什么,解决的问题

    // 1. 实体类 class User{ } //2. dao class  UserDao{ .. 访问db } //3. service class  UserService{ UserDao ...

  9. .net core 中简单封装Dapper.Extensions 并使用sqlsuger自动生成实体类

    引言 由公司需要使用dapper  同时支持多数据库 又需要支持实体类 又需要支持sql 还需要支持事务 所以采用了 dapper + dapperExtensions  并配套 生成实体类小工具的方 ...

  10. you have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'varchar(255), sort integer not null

    you have an error in your SQL syntax; check the manual that corresponds to your MySQL server version ...