android的数据存储主要分为三类:
本文将主要介绍内部存储的相关知识:
- 写操作: 如何将数据存储到应用内部
- 读操作:如何将数据存储回显出来
文件存储的路径:/data/data//files(此处的applicationID一般为包名)【也可用File filesDir = this.getFilesDir();获取】
——————————————————————————————————————————
效果展示:(仿QQ登录界面)
1、写操作
注意事项: 一般来说读写操作都要对获取到的内容进行一个判空处理
- cache dir is /data/user/0/com.example.qqlogindemo/cache
- 缓存路径,用于保存缓存文件,目录下的文件由系统根据存储情况进行清理,如果不够用了就会被清理
- files dir is /data/user/0/com.example.qqlogindemo/files
- 文件路径,可以用代码删除,也可以通过设置里的应用列表选项来清理
步骤总结:
- 获取文件保存的路径:File filesDir = this.getFilesDir();
- 新建文件:File saveFile=new File(filesDir,“info.text”);
- 打开文件:FileOutputStream fos=new FileOutputStream(saveFile);
- 写操作:fos.write(().getBytes());
- 关闭文件: fos.close();
2、读操作
每次重新写入的数据都会覆盖上一次写入的数据,这样就可以实现记住密码、记住账号的功能
步骤总结:
- 利用文件流的API打开文件:FileInputStream fileInputStream=this.openFileInput(“info.text”);
- 通过buffrereader来读取内容:BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(fileInputStream));
String info=bufferedReader.readLine(); - 回显数据:mAccount.setText(account);
————————————————————————————————————————————————————————————————————
源码:
public class MainActivity extends AppCompatActivity {
private static final String TAG ="MainActivity" ;
private EditText mAccount;
private EditText mPassword;
private Button mLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView(); //找到控件
initListener(); //设置点击事件
}
private void initListener() {
mLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG,"点击了登录");
HandlerLoginEvent(v); //处理登录事件
}
});
}
@Override
protected void onResume() {
super.onResume();
//获取数据
// File fileDir=this.getFilesDir();
// File saveFile=new File(fileDir,"info.text");
try {
FileInputStream fileInputStream=this.openFileInput("info.text");
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(fileInputStream));
String info=bufferedReader.readLine();
//保存时的格式:账号***密码,所以读取的时候要对数据进行切割
//fos.write((accountText+"***"+passwordText).getBytes());
String[] splits=info.split("\\*\\*\\*"); //利用string的split方法以“***”为界限进行切割
// 但是*是一个特殊符号所以要进行转义,切割后得到两个字符串就用一个数组存放
String account=splits[0];
String password=splits[1];
//回显数据
mAccount.setText(account);
mPassword.setText(password);
} catch (Exception e) {
e.printStackTrace();
}
}
private void HandlerLoginEvent(View v) {
//拿到用户输入的账号和密码
String accountText=mAccount.getText().toString();
String passwordText=mPassword.getText().toString();
//对账号进行检查,一般来说,在实际开发中,需要对用户输入的账号密码进行合法性检查(长度、敏感词等)
//此处只进行一个判空处理
// if(accountText.length() == 0){
// //账号的长度为空
// Toast.makeText(MainActivity.this,"账号不能为空",Toast.LENGTH_SHORT).show();
// return;
// }
// if (passwordText.length() == 0){
// //密码为空
// Toast.makeText(MainActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
// return;
// }
if (TextUtils.isEmpty(accountText)){
Toast.makeText(MainActivity.this,"账号不能为空",Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(passwordText)) {
Toast.makeText(MainActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
return;
}
//把账号和密码保存起来
saveUserInfo(accountText,passwordText);
}
private void saveUserInfo(String accountText, String passwordText) {
Log.d(TAG,"保存用户信息");
/**
* cache dir is /data/user/0/com.example.qqlogindemo/cache
* 缓存路径,用于保存缓存文件,目录下的文件由系统根据存储情况进行清理,如果不够用了就会被清理
* files dir is /data/user/0/com.example.qqlogindemo/files
* 文件路径,可以用代码删除,也可以通过设置里的应用列表选项来清理
*/
//获取到缓存文件的路径
File cacheDir=this.getCacheDir();
Log.d(TAG,"cache dir is "+cacheDir);
//获取到文件保存的路径
File filesDir = this.getFilesDir();
Log.d(TAG,"files dir is "+filesDir);
File saveFile=new File(filesDir,"info.text");
try {
//File file=new File("/data/data/com.example.qqlogindemo/info.text");
if (!saveFile.exists()){
saveFile.createNewFile();}
FileOutputStream fos=new FileOutputStream(saveFile);
//以特定的格式来存储
//账号***密码
fos.write((accountText+"***"+passwordText).getBytes());
fos.close();
Toast.makeText(MainActivity.this,"数据保存成功",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
private void initView() {
mAccount = findViewById(R.id.et_account);
mPassword = findViewById(R.id.et_password);
mLogin = findViewById(R.id.btn_login);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#70000000">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="30dp"
android:layout_marginTop="80dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QQ"
android:textColor="#ffffff"
android:textSize="40sp"
android:textStyle="bold"
android:drawableLeft="@mipmap/icon"
android:drawablePadding="15dp"/>
<EditText
android:id="@+id/et_account"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="QQ号码/手机号码/邮箱"
android:textColorHint="#ffffff"
android:layout_marginTop="30dp"/>
<EditText
android:id="@+id/et_password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="密码"
android:textColorHint="#ffffff"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LOGIN"
android:layout_marginTop="20dp"
android:textSize="18sp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="忘记密码"
android:textSize="16sp"
android:textColor="#ffff90"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新用户注册"
android:layout_alignParentRight="true"
android:textSize="16sp"
android:textColor="#ffff90"/>
</RelativeLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录即代表阅读并同意"
android:textColor="#FFFFFF"
android:textSize="22sp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>