最全3种安卓存储数据解析—SharedPreferenc存储(实现自动登录)+文件存储(模拟备忘录)+sqlite数据库(模拟用户登录与管理)
今天来说说安卓存储数据的三种方式的特点与其基本操作
1、SharedPreferenc存储
特点:用来以最简单的方式对数据进行永久保存的方法。hml文件格式存储
使用Shared Preferenc存储数据的步骤
录入数据
1、获取SharedPreferenc对象
(1)getSharedPreferences(string 存储文件名称,int 访问权限由安卓提供的常量);
如SharedPreferenc sp=getSharedPreferences("test",MODE_PRIVATE);
(2)getPreferences(int 访问权限由安卓提供的常量);
2、获得SharedPreferences.Editor对象
SharedPreferences.Editor editor= sp.edit();
3、向SharedPreferences.Editor对象添加数据
putString()
putint()等
editor.commit();
读取数据
1、获取SharedPreferenc对象
getSharedPreferences();
2、使用SharedPreferenc对象提供的getxxx()获取
getString();等
下面来看例子:
代码与详解
package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
String name="csdn";
String password="123"; //我直接定义自动登录的账号密码,你们也可以从其他地方获取,然后定义
EditText editText1;
EditText editText2;
Button button ;
//登录按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1=findViewById(R.id.e1);
editText2=findViewById(R.id.e2);
button=findViewById(R.id.button);
final SharedPreferences shared=getSharedPreferences("sharedtest",MODE_PRIVATE);
String str1= shared.getString("name",null);
String str2= shared.getString("password",null); //第一个参数为KEY等同于他的名字,第二个参数设置KEY的默认值为空
//自动登录
if(str1!=null&&str2!=null&&str1.equals(name)&&str2.equals(password)){
Intent intent =new Intent(MainActivity.this, jumptest.class);
startActivity(intent);
}
//手动登录,并保存账号秘密
else{
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String in_name=editText1.getText().toString();
String in_password=editText2.getText().toString();
SharedPreferences.Editor edit=shared.edit(); //获取Editor对象
if (in_name.equals(name)&&in_password.equals(password)){
edit.putString("name",in_name); //保存账号
edit.putString("password",in_password); //保存密码
edit.commit(); //提交信息
Intent intent =new Intent(MainActivity.this, jumptest.class);
startActivity(intent);
}
else Toast.makeText(MainActivity.this,"账号或密码错误",Toast.LENGTH_SHORT).show();
}
});
}
}
}
2、文件存储
文件存储分为内部存储和外部存储(也是在手机当中)今天主要讲讲文件存储的内部存储
——模拟备忘录
内部存储
特点:
1、默认只能被创建他的应用访问到
2、应用被卸载后,内部储存的文件也被删除
3、一旦内部存储空间耗尽,手机也就无法使用
下面是要点:
写入文件
1、获取FileOutputStream对象
FileOutputStream f=null;
f=openFileOutput("文件名",int 访问权限由安卓提供的常量);
2、调用write()方法
3、调用flush()清除缓存
4、调用close()方法 关闭输出流对象 应先判断其是否为空
读取文件
1、获取FileInputStream对象
2、调用read()方法 注意字节与字符串之间的转换
3、调用close()方法 关闭输入流对象 应先判断其是否为空
模拟备忘录,具体详解看代码
package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
Button save;
Button read;
EditText editText;
byte by[]=null;
FileInputStream re=null; //读文件
FileOutputStream f=null; //写文件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
save=findViewById(R.id.button); //保存按钮
read=findViewById(R.id.button2); //读取按钮
editText=findViewById(R.id.editText);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String str=editText.getText().toString();
f=openFileOutput("test",MODE_PRIVATE); //获取输出流FileOutputStream对象 第一个参数为文件名,第二个参数为访问权限
f.write(str.getBytes()); //把字符串变成字符数组进行储存
f.flush(); //清除缓存
Toast.makeText(MainActivity.this,"保存成功",Toast.LENGTH_LONG).show();;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if(f!=null) { //关闭输出流对象
try {
f.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
read.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
re=openFileInput("test");
by=new byte[re.available()]; //实例化字节数组
re.read(by); //读取数据
String str=new String(by); //转换为字符串
editText.setText(str); //在编辑框中显示
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (re!=null) {
try {
re.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
}
}
数据库存储 SQLite
优点:占用资源少,运行效率高,可移植性强,安全可靠
由于数据库存储 SQLite介绍篇幅过长,我将它写成了另一篇博客
具体请移步:
https://blog.csdn.net/weixin_44758662/article/details/106863183