基础学习总结(三)--文本、SD卡数据读写
简单的文本数据写入文件不需要权限,读写SD卡在4.0版本前需要写权限、在4.0后需要读写权限
布局:
<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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity" > <EditText android:id="@+id/et_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:phoneNumber="true"
android:hint="请输入手机号" /> <EditText
android:id="@+id/et_pwd"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:hint="请输入密码"
android:inputType="textPassword" /> <CheckBox android:id="@+id/cb_remerber_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="记住密码" /> <Button android:id="@+id/btn_Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆"
/> </LinearLayout>
Manifest.xml添加权限
<!-- 写入外部设备(sdka)的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
MainActivity:
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "MainActivity";
private EditText etNumber;
private EditText etPwd;
private CheckBox cbRemerber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件中数据
etNumber = (EditText)findViewById(R.id.et_number);
etPwd=(EditText)findViewById(R.id.et_pwd);
cbRemerber=(CheckBox)findViewById(R.id.cb_remerber_pwd);
Button btnLogin = (Button)findViewById(R.id.btn_Login);
//添加点击事件
btnLogin.setOnClickListener(this);
//获取SD卡数据
Map<String,String> userInfo=UtilsOfSDCard.getUserInfo(this);
if(userInfo!=null){
etNumber.setText(userInfo.get("number"));
etPwd.setText(userInfo.get("pwd"));
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//1.取出账号,密码
String Number=etNumber.getText().toString();
String Pwd=etPwd.getText().toString();
//判断是否有值,弹出吐司
if(TextUtils.isEmpty(Number) || TextUtils.isEmpty(Pwd)){
Toast.makeText(this, "请正确数据账号、密码", Toast.LENGTH_SHORT).show();
return;
}
//2.判断cb是否被选中
if(cbRemerber.isChecked()){
Log.i(TAG, "记住密码"+Number+","+Pwd);
boolean isSuccess=UtilsOfSDCard.saveUserInfo(this,Number, Pwd);
if(isSuccess)
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
}
//3.登陆成功
Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
}
}
UtilsOfSDCard类:
public class UtilsOfSDCard {
//保存信息到sd卡
public static boolean saveUserInfo(Context context, String number,String pwd){
try {
//获取SD卡状态
String state=Environment.getExternalStorageState();
//判断SD卡状态
if(!Environment.MEDIA_MOUNTED.equals(state))
{
return false;
}
//动态获取SD卡路径
File sdCardFile = Environment.getExternalStorageDirectory();
File f=new File(sdCardFile,"qqLogin1.txt");
//写入流
FileOutputStream fos=new FileOutputStream(f);
String data=number+"##"+pwd;
fos.write(data.getBytes());
fos.flush();
fos.close();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
//返回信息
public static Map<String,String> getUserInfo(Context context){
try {
String state=Environment.getExternalStorageState();
if(!Environment.MEDIA_MOUNTED.equals(state))
{
return null;
}
File sdCartFile=Environment.getExternalStorageDirectory();
File f=new File(sdCartFile,"qqLogin1.txt");
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String text=br.readLine();
br.close();
if(!TextUtils.isEmpty(text)){
Map<String,String> userInfoMap=new HashMap<String,String>();
String[] split = text.split("##");
userInfoMap.put("number", split[0]);
userInfoMap.put("pwd", split[1]);
Log.w("UtilsOfSDCard", split[0]+","+split[1]);
return userInfoMap;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
Utils类:
public class Utils {
/*
* 保存用户信息
* */
public static boolean saveUserInfo(String number,String pwd){
try {
String path="/data/data/com.cui.qqlogin/QQLogin.txt";
//写入流
FileOutputStream fs=new FileOutputStream(path);
String data=number+"##"+pwd;
fs.write(data.getBytes());
fs.flush();
fs.close();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public static boolean saveUserInfo(Context context, String number,String pwd){
try {
//动态获取文件路径
File filesDir = context.getFilesDir();
File f=new File(filesDir,"QQLogin.txt");
//写入流
FileOutputStream fs=new FileOutputStream(f);
String data=number+"##"+pwd;
fs.write(data.getBytes());
fs.flush();
fs.close();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public static Map<String,String> getUserInfo(){
try {
String path="/data/data/com.cui.qqlogin/QQLogin.txt";
//输出流
FileInputStream fis=new FileInputStream(path);
//字符流
BufferedReader reader=new BufferedReader(new InputStreamReader(fis));
String text = reader.readLine();
if(!TextUtils.isEmpty(text)){
String[] split=text.split("##");
Map<String,String> userofMap=new HashMap<String,String>();
userofMap.put("number", split[0]);
userofMap.put("pwd", split[1]);
return userofMap;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static Map<String,String> getUserInfo(Context context){
try {
//String path="/data/data/com.cui.qqlogin/QQLogin.txt";
//动态获取文件路径
File filesDir = context.getFilesDir();
File f=new File(filesDir,"QQLogin.txt");
//输出流
FileInputStream fis=new FileInputStream(f);
//字符流
BufferedReader reader=new BufferedReader(new InputStreamReader(fis));
String text = reader.readLine();
reader.close();
if(!TextUtils.isEmpty(text)){
String[] split=text.split("##");
Map<String,String> userofMap=new HashMap<String,String>();
userofMap.put("number", split[0]);
userofMap.put("pwd", split[1]);
return userofMap;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
====================插播快捷键
Ctrl+N 快速新建
Ctrl+H 选中项目中搜索
Ctrl+L 跳转到指定行
Ctrl+F 在文本中查找内容
====================
文件存储地址:
this.getFilesDir(); // /data/data/包名/files
this.getCacheDir(); // /data/data/包名/cache
openFileOutput("aa.txt", 0); /data/data/包名/files/aa.txt
context.getSharedPreferences("cuidemo", context.MODE_PRIVATE); /data/data/包名/shared_prefs/cuidemo.xml
public class UtilsOfSharedPreferences {
//保存信息到sd卡
public static boolean saveUserInfo(Context context, String number,String pwd){
try {
sp=context.getSharedPreferences("cuidemo", context.MODE_PRIVATE);
//获得一个编辑对象
Editor edit = sp.edit();
edit.putString("number", number);
edit.putString("pwd", pwd);
edit.commit();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
//返回信息
public static Map<String,String> getUserInfo(Context context){
SharedPreferences sp=context.getSharedPreferences("cuidemo", context.MODE_PRIVATE);
String number=sp.getString("number", null);
String pwd=sp.getString("pwd", null);
if(!TextUtils.isEmpty(number)&& !TextUtils.isEmpty(pwd)){
Map<String,String> userInfoMap=new HashMap<String,String>();
userInfoMap.put("number", number);
userInfoMap.put("pwd", pwd);
return userInfoMap;
}
return null;
}
}
权限相关:
1. 私有文件
Context.MODE_PRIVATE
2. 可读文件
Context.MODE_WORLD_READABLE
3. 可写文件
Context.MODE_WORLD_WRITEABLE
4. 可读可写文件.
Context.MODE_WORLD_WRITEABLE+Context.MODE_WORLD_READABLE
权限二进制图解:

基础学习总结(三)--文本、SD卡数据读写的更多相关文章
- JavaScript 基础 学习(三)
JavaScript 基础 学习(三) 事件三要素 1.事件源: 绑定在谁身上的事件(和谁约定好) 2.事件类型: 绑定一个什么事件 3.事件处理函数: 当行为发生的时候,要执行哪一个函数 ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(三)-SD卡的操作流程
其他链接 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 ...
- 使用STM32F103ZET霸道主板实现SD卡的读写(非文件系统)
了解STM32F103ZET是高容量多管脚的芯片 了解SD读写线路图 了解SD的基地址 阅读STM32F10xx英文参考 SDIO那章,我们编写代码边看文档解析 建工程,打开包含所有包括外设库函数的样 ...
- 【译】如何在 Android 5.0 上获取 SD卡 的读写权限
因为最近项目需要,涉及到 SD卡 的读写操作,然而申请 <!-- 读写权限 --> <uses-permission android:name="android.permi ...
- SD卡spi读写流程
SD卡spi读写流程 1.SD卡的命令格式: SD卡的指令由6字节(Byte)组成,如下: Byte1:0 1 x x x x x x(命令号,由指令标志定义,如CMD39为100111即16进制0x ...
- 快速解决设置Android 23.0以上版本对SD卡的读写权限无效的问题
快速解决设置Android 23.0以上版本对SD卡的读写权限无效的问题 转 https://www.jb51.net/article/144939.htm 今天小编就为大家分享一篇快速解决设置And ...
- 用 S5PV210 学习 Linux (三) SD卡下载
学习地址:http://edu.51cto.com/lesson/id-63015.html http://blog.csdn.net/karven_/article/details/52015325 ...
- 基于stm32f103zet6的FAT16文件系统学习0(读SD卡扇区)
SD卡已经看了两天了,主要是因为测试出来的卡容量不对,所以一直找原因,最终还是发现了,总比不过是单位上面出现了问题,或许是之前没有接触到SD的缘故吧,所以对其中的一些寄存器很不了解,一切都是重新开始, ...
- java网络爬虫基础学习(三)
尝试直接请求URL获取资源 豆瓣电影 https://movie.douban.com/explore#!type=movie&tag=%E7%83%AD%E9%97%A8&sort= ...
随机推荐
- restful php
http://bbs.phpchina.com/thread-228725-1-1.html http://www.cnblogs.com/artech/p/3506553.html http://w ...
- Helpers\Tags
Helpers\Tags The tags helper is a collection of useful methods: Tags::clean($data) Clean function to ...
- poj1068解题报告(模拟类)
POJ 1068,题目链接http://poj.org/problem?id=1068 题意: 对于给出给出的原括号串S,对应两种数字密码串P.W: S (((()()()))) P- ...
- Android 高级UI设计笔记03:使用ListView实现左右滑动删除Item
1. 这里就是实现一个很简单的功能,使用ListView实现左右滑动删除Item: (1)当我们在ListView的某个Item,向左滑动显示一个删除按钮,用户点击按钮,即可以删除该项item,并且有 ...
- Nginx 403 error
nginx 的 403 Forbidden errors 表示你在请求一个资源文件但是nginx不允许你查看.403 Forbidden 只是一个HTTP状态码,像404,200一样不是技术上的错误. ...
- 对于android触摸事件模型的一些理解
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- PL/SQL developer 使用技巧汇总
为了快速的使用PL/SQL developer 进行 oracle数据库相关开发,将一些使用频率较高的使用技巧进行汇总如下,以下转自网络和自己的测试 1.切换schema --switch schem ...
- angularjs $state.go 传参
在目标页面规定接受的参数:$stateProvider.state('page2', {params: {'data': null}}) 传参:$state.go('page2', {data: 'a ...
- js如何判断一个数组中是否有重复的值
引自:http://bbs.tianya.cn/post-414-38497-1.shtml 方法一: var ary = new Array("111","22&quo ...
- Math.random获得随机数
function GetRandomNum(Min,Max){ var Range = Max - Min; var Rand = Math.random(); ...