◆Android文件系统
◆数据存储的方式概述
在Android SDK中关于数据存储一共有以下几种:
◆内部存储
◆存储在手机内存中,适合保存一些是不太大的临时数据
◆外部存储(存储在SD卡中)
◆基本上什么数据都可以保存
◆Sqlite数据库
◆适合保存有一定规则的数据
◆Shared Preferences
◆存储在APP安装目录下,会生成一个xml文件,只能本程序访问
◆适合存储简单零散的数据
◆网络存储
◆从服务器获取数据
◆Content Provider(内容提供者)应用与应用数据共享的通道
◆数据存储方式1:内部存储(存储在手机内存中)
实例:用户登录程序
关键代码
写入用户名并读取用户名
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/edt_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请在此输入用户名"/>I
<EditText
android:id="@+id/edt_pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请在此输入密码"/>I
<CheckBox
android:id="@+id/checkbox"
android:text="记住用户名"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn1"
android:onClick="btnClick"
android:text="登录"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.java
package com.bluelesson.a303;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initCtrl();
initCheckBox();
}
//这个函数在打开app时,会自动读取config.txt中的用户名
private void initCtrl() {
//获取用户名界面
EditText editText =findViewById(R.id.edt_user);
String user = editText.getText().toString();
//读取配置文件中用户名,设置对象
//String path="/data/data/com.bluelesson.a303/config.txt";
String packgeName = getPackageName();
//String path = "/data/data"+File.separator+packgeName+File.separator+"config.txt";
String filesDir = getFilesDir().getPath();
String path = filesDir+File.separator+"config.txt";
File file=new File(path);
int fileLen = (int)file.length();
try {
FileInputStream inputStream=new FileInputStream(file);
byte bytes[] = new byte[fileLen];
inputStream.read(bytes);
inputStream.close();
String s = new String(bytes);
editText.setText(s);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//这个函数在密码不对时,也会保存用户名
private void initCheckBox() {
//1.获取界面对象
CheckBox checkBox=findViewById(R.id.checkbox);
//2.设置监听器
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//获取用户名
EditText editText =findViewById(R.id.edt_user);
String user = editText.getText().toString();
//保存用户名
//String path="/data/data/com.bluelesson.a303/config.txt";
String fileDir = getFilesDir().getPath();
String path = fileDir+File.separator+"config.txt";
File file=new File(path);
try {
FileOutputStream outputStream=new FileOutputStream(file);
byte bytes[] = user.getBytes();
outputStream.write(bytes);
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
//这个函数在密码正确时,并在选中了保存时,才会保存用户名
public void btnClick(View view) {
//获取用户名
EditText editText =findViewById(R.id.edt_user);
String user = editText.getText().toString();
//获取用户名
EditText editText1 =findViewById(R.id.edt_pass);
String pass = editText1.getText().toString();
//模拟判断登录情况
if(!user.equals(pass)){
return;
}
Toast.makeText(this,"成功登录",Toast.LENGTH_SHORT).show();
//1.获取界面对象
CheckBox checkBox=findViewById(R.id.checkbox);
//2.判断情况
//checkBox.setOnCheckedChangeListener();
if(checkBox.isChecked()){
//保存用户名
//String path="/data/data/com.bluelesson.a303/config.txt";
String fileDir = getFilesDir().getPath();
String path = fileDir+File.separator+"config.txt";
File file=new File(path);
try {
FileOutputStream outputStream=new FileOutputStream(file);
byte bytes[] = user.getBytes();
outputStream.write(bytes);
outputStream.clone();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
相关获取方法
◆数据存储方式2:外部存储(存储在SD卡中)
SD卡权限
关键代码
MainActivity.java
package com.bluelesson.a303;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initCtrl();
//initCheckBox();
initCheckBox1();
}
//这个函数相当于同时实现了 initCtrl和initCheckBox两个函数的功能
private void initCheckBox1() {
//1.读取配置文件
//获取用户名界面
EditText editText =findViewById(R.id.edt_user);
String user = editText.getText().toString();
//读取配置文件中用户名,设置对象
String path = Environment.getExternalStorageDirectory()+File.separator+"config.txt";
File file=new File(path);
int fileLen = (int)file.length();
try {
FileInputStream inputStream=new FileInputStream(file);
byte bytes[] = new byte[fileLen];
inputStream.read(bytes);
inputStream.close();
String s = new String(bytes);
editText.setText(s);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//2.设置监听器写入配置文件
//1.获取界面对象
CheckBox checkBox=findViewById(R.id.checkbox);
//2.设置监听器
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//获取用户名
EditText editText =findViewById(R.id.edt_user);
String user = editText.getText().toString();
//保存用户名
//String path="/data/data/com.bluelesson.a303/config.txt";
//String fileDir = getFilesDir().getPath();
String state = Environment.getExternalStorageState();
//判断一下SD卡状态
if(state.equals(Environment.MEDIA_UNMOUNTED)){
Toast.makeText(MainActivity.this,"请插入SD卡",Toast.LENGTH_SHORT).show();
return;
}
String path = Environment.getExternalStorageDirectory()+File.separator+"config.txt";
File file=new File(path);
try {
FileOutputStream outputStream=new FileOutputStream(file);
byte bytes[] = user.getBytes();
outputStream.write(bytes);
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}