Android TextUtils工具类的使用
1.采用File类,在指定目录下读写数据
java后台代码为:
(1)向app的/data/data/com.example.lucky.helloworld目录下写入文本(涉及IO的读写操作)
package com.example.lucky.helloworld; 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.HashMap;
import java.util.Map; public class UserInfoUtils {
public static boolean saveInfo(String username,String password){
String result=username+"=="+password;
//创建File类,指定数据存储位置
File file=new File("/data/data/com.example.lucky.helloworld/info.txt");
try {
//创建一个文件的输出流
FileOutputStream fos=new FileOutputStream(file);
fos.write(result.getBytes()); //向文件中写入数据
fos.close(); //关闭文件
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
} //读取用户信息
public static Map<String,String> readInfo(){
//定义map
Map<String,String> map=new HashMap<>();
File file=new File("/data/data/com.example.lucky.helloworld/info.txt");
try {
FileInputStream fis=new FileInputStream(file);
BufferedReader br=new BufferedReader(new InputStreamReader(fis));
String strTemp=br.readLine(); //读取数据
String[] splitstr=strTemp.split("=="); //对字符串进行切割
String username=splitstr[0];
String password=splitstr[1];
map.put("name",username); //将数据放入map集合中
map.put("pw",password);
fis.close(); //关闭数据流
return map;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
(2)MainActivity.java代码
package com.example.lucky.helloworld; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; import java.util.Map; public class MainActivity extends AppCompatActivity {
EditText et_username;
EditText et_password;
Button bt_login; //当Activityq启动时就会执行onCreate方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置activity的内容,内容来自布局文件activity_main.xml
setContentView(R.layout.activity_main);
Log.v("001","我是verbose级别");
Log.i("001","我是info级别");
Log.d("001","我是debug级别");
Log.w("001","我是warn级别");
Log.e("001","我是error级别");
System.out.println("sout输出");
et_username=findViewById(R.id.et_username);
et_password=findViewById(R.id.et_password);
bt_login=findViewById(R.id.bt_login); //读取/data/data/com.example.lucky.helloworld/info.txt文件
Map<String,String> map=UserInfoUtils.readInfo();
if(map!=null){
String name=map.get("name");
String password=map.get("pw");
et_username.setText(name); //将读取的数据显示出来
} bt_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username=et_username.getText().toString();
String password=et_password.getText().toString();
//安卓特有的工具类,用来判断string是否为空
if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){
Toast.makeText(MainActivity.this,"用户名不能为空",Toast.LENGTH_SHORT).show();
}else {
boolean result=UserInfoUtils.saveInfo(username,password);
if(result){
Toast.makeText(MainActivity.this,"数据存储成功",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"数据存储失败",Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
2.利用Context类获取常用目录,存储文件(推荐使用)
文件数据保存的模式:
1)MODE_PRIVATE ,这个模式用得最多,其他的模式很少用
2)MODE_APPEND
3)MODE_WORLD_READABLE
4)MODE_WORLD_WRITEABLE
(1)读写工具类
package com.example.lucky.helloworld; import android.content.Context; 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.HashMap;
import java.util.Map; public class UserInfoUtils {
public static boolean saveInfo(Context context, String username, String password){
String result=username+"=="+password; try {
//创建一个文件的输出流,使用上下文获取常用目录
FileOutputStream fos=context.openFileOutput("info2.txt",Context.MODE_PRIVATE);
fos.write(result.getBytes()); //向文件中写入数据
fos.close(); //关闭文件
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
} //读取用户信息
public static Map<String,String> readInfo(Context context){
//定义map
Map<String,String> map=new HashMap<>();
try {
FileInputStream fis=context.openFileInput("info2.txt");
BufferedReader br=new BufferedReader(new InputStreamReader(fis));
String strTemp=br.readLine(); //读取数据
String[] splitstr=strTemp.split("=="); //对字符串进行切割
String username=splitstr[0];
String password=splitstr[1];
map.put("name",username); //将数据放入map集合中
map.put("pw",password);
fis.close(); //关闭数据流
return map;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
(2)MainActivity.class
package com.example.lucky.helloworld;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Map; public class MainActivity extends AppCompatActivity {
EditText et_username;
EditText et_password;
Button bt_login;
Button bt_private;
Button bt_append;
Button bt_readable;
Button bt_writeable; //当Activityq启动时就会执行onCreate方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置activity的内容,内容来自布局文件activity_main.xml
setContentView(R.layout.activity_main);
init(); //info2.txt文件
Map<String,String> map=UserInfoUtils.readInfo(MainActivity.this);
if(map!=null){
String name=map.get("name");
String password=map.get("pw");
et_username.setText(name); //将读取的数据显示出来
} bt_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username=et_username.getText().toString();
String password=et_password.getText().toString();
//安卓特有的工具类,用来判断string是否为空
if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){
Toast.makeText(MainActivity.this,"用户名不能为空",Toast.LENGTH_SHORT).show();
}else {
boolean result=UserInfoUtils.saveInfo(MainActivity.this,username,password);
if(result){
Toast.makeText(MainActivity.this,"数据存储成功",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"数据存储失败",Toast.LENGTH_SHORT).show();
}
}
}
}); //在data/data/com.example.lucky.helloworld下创建files文件夹,生成属性为private的文件
bt_private.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
FileOutputStream fos=openFileOutput("private.txt",MODE_PRIVATE); //创建private.txt
fos.write("private".getBytes()); //写入数据
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}); //在data/data/com.example.lucky.helloworld下创建files文件夹,生成属性为append(可追加内容)的文件
bt_append.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
FileOutputStream fos=openFileOutput("append.txt",MODE_APPEND);
fos.write("append".getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}); //在data/data/com.example.lucky.helloworld下创建files文件夹,生成属性为MODE_WORLD_READABLE的文件,只读模式
bt_readable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
FileOutputStream fos=openFileOutput("readable.txt",MODE_WORLD_READABLE);
fos.write("readable".getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}); bt_writeable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
FileOutputStream fos=openFileOutput("writeable.txt",MODE_WORLD_WRITEABLE);
fos.write("writeable".getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
} private void init() {
et_username=findViewById(R.id.et_username);
et_password=findViewById(R.id.et_password);
bt_login=findViewById(R.id.bt_login);
bt_private=findViewById(R.id.bt_private);
bt_append=findViewById(R.id.bt_append);
bt_readable=findViewById(R.id.bt_readable);
bt_writeable=findViewById(R.id.bt_writeable);
}
}
Android TextUtils工具类的使用的更多相关文章
- 53. Android常用工具类
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefer ...
- Android 常见工具类封装
1,MD5工具类: public class MD5Util { public final static String MD5(String s) { char hexDigits[] = { '0' ...
- 【转】Android常用工具类
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefe ...
- Android基础工具类重构系列一Toast
前言: 一直在考虑写一下Android实际项目中的一些总结,翻看CSDN博客,上一篇已经是一年多曾经. 本系列定位Android基础工具类重构.旨在记录实际项目中经经常使用到的一些工具类,比方Toas ...
- (转载)android 一些工具类汇总
android 一些工具类汇总 作者:曾田生z 字体:[增加 减小] 类型:转载 时间:2016-08-14我要评论 本文给大家汇总介绍了一些常用的Android工具类,非常的简单实用,有需要的小伙伴 ...
- Android FileUtil(android文件工具类)
android开发和Java开发差不了多少,也会有许多相同的功能.像本文提到的文件存储,在Java项目和android项目里面用到都是相同的.只是android开发的一些路径做了相应的处理. 下面就是 ...
- android开发工具类总结(一)
一.日志工具类 Log.java public class L { private L() { /* 不可被实例化 */ throw new UnsupportedOperationException ...
- Android 开发工具类 35_PatchUtils
增量更新工具类[https://github.com/cundong/SmartAppUpdates] import java.io.File; import android.app.Activity ...
- Android Joda-time工具类
Joda-Time提供了一组Java类包用于处理包括ISO8601标准在内的date和time.可以利用它把JDK Date和Calendar类完全替换掉,而且仍然能够提供很好的集成. Joda- ...
随机推荐
- js的简单介绍及基本用法
1. JS的简介 概述: JavaScript, 是一门弱类型语言, 用来给页面增加动态功能的. //弱类型语言: 对数据的数据类型划分不精细(不明确). 特点: A. JavaScript 是一种轻 ...
- Oracle树查询,start with connect by prior 递归查询用法(转载)
本人觉得这个写的真不错,实用性强,就转载过来了 这个子句主要是用于B树结构类型的数据递归查询,给出B树结构类型中的任意一个结点,遍历其最终父结点或者子结点. 先看原始数据: 1 create tabl ...
- mongo 修改器 $inc/$set/$unset/$pop/$push/$pull/$addToSet
mongo $inc 可以对集合里面的某些值是数字的增减.看代码 $set 可以进行修改,并且不存在的时候默认添加. 同时还能该变数据的类型. 还可以该变内嵌元素的值 用.调用 $unset 删除 ...
- 2014蓝桥杯B组初赛试题《六角填数》
题目描述: 如图[1.png]所示六角形中,填入1~12的数字. 使得每条直线上的数字之和都相同. 图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少? 请通过浏览器提交 ...
- SpringBoot26 RestTemplate、WebClient
1 RestTemplate RestTemplate是在客户端访问 Restful 服务的一个核心类:RestTemplate通过提供回调方法和允许配置信息转换器来实现个性化定制RestTempla ...
- 用CSS3.0画圆
CSS3.0中有一个border-radius属性,这个属性允许向 div 元素添加圆角边框,也就是div边角不再一直是直角,在CSS3.0中可以做成圆角了,所以我们可以用这个属性用div画一个圆,或 ...
- 《the art of software testing》 第三章 人工测试
在深入研究较为传统的计算机测试技术之前,要先进行"人工测试". 代码检查与走查是两种主要的人工测试方法. 代码检查与走查是对过去桌面检查过程(在提交测试前由程序员阅读自己程序的过程 ...
- linux学习1----初涉linux
linux因其稳定高效的特点,受到很多开发者的青睐,因此将其作为服务器的操作系统. 作为一名开发者,程序员,掌握了一定的linux知识和技巧,程序的开发部署和运行也有不小的帮助. linux由于其开源 ...
- UML类之间的关系
原文:http://www.cnblogs.com/me115/p/4092632.html 下面详细介绍这六种关系: 类之间的关系 泛化关系(generalization) 类的继承结构表现在UML ...
- 第16章-使用Spring MVC创建REST API
1 了解REST 1.1 REST的基础知识 REST与RPC几乎没有任何关系.RPC是面向服务的,并关注于行为和动作:而REST是面向资源的,强调描述应用程序的事物和名词. 为了理解REST是什么, ...