14.3.01


数据存储(持久化)  

五种:(外存)

1Shared Preferences

Store private primitive data in key-value pairs.

参数共享(保存关键参数:下载、登陆等)

2Internal Storage

Store private data on the device memory.

内部存储

3External Storage

Store public data on the shared external storage.

外部存储(保存文件、安装包)

4SQLite Databases

Store structured data in a private database.

数据库存储(外存)

5Network Connection

Store data on the web with your own network server.

网络存储



1、sharePreferences

应用场景:应用程序有少量的数据需要保存(设置参数的保存)


例子:按钮点击保存

//得到参数共享类型的对象

   SharePreferences preference=getPreferences(0);//0:默认值

//编辑器类型的对象

Editor edit=Preference.edit();

//想编辑器对象添加数据

Edit.putBoolean(isLogin,true);

edit.putString(name,Lucy);

//提交

   Commit();



获取数据

getBoolean(isLogin,false);//false:默认值

getString(name,SS);//SS:默认值

   privatevoid getData() {

   SharedPreferences preferences = getPreferences(0);

   boolean login = preferences.getBoolean("isLogin", false);

   String name = preferences.getString("name", "Lily");

   mTv_show.setText(login+"  "+name);

}


保存位置

Data/data/包名/shared_prefs/xxx.xml

修改保存位置:

Sharepreferences sp=getSharepreferrences(xibo,0);//xibo保存的文件名





2、InternalStorage

保存位置:

Data/data/包名/files/aa.txt"

保存


privatevoid saveData() {

FileOutputStream openFileOutput =null;

try {

openFileOutput = openFileOutput("aa.txt", MODE_PRIVATE);

String str="Hello";

openFileOutput.write(str.getBytes());

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally{

if(openFileOutput!=null){

try {

openFileOutput.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}



读取

FileInputStream

privatevoid getData() {

FileInputStream fis=null;

ByteArrayBuffer bab=new ByteArrayBuffer(4000);

try {

Log.e("getData", "sss");

fis=new FileInputStream("data/data/com.example.day_example/files/aa.txt");

int len;

byte[] buffer=newbyte[1024];

while(-1!=(len=fis.read(buffer))){

bab.append(buffer, 0, len);

}

String str=new String(bab.toByteArray(), 0, bab.length());

mTv_show.setText(str);  

Log.e("getData------>", ""+bab.length());

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally{

if(fis!=null){

try {

fis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}


读写模式:

MODE_PRIVATE私有模式:只有当前应用才能对这个文件进行读写操作,不会进 行叠加,每次都清空

MODE_APPEND添加模式:只有当前应用才能对这个文件进行读写操作,对数据  追加

MODE_WORLD_READABLE其他应用可读

MODE_WORLD_WRITEABLE其他应用可写


3、外部读取


查找sd

   File file =Environment.getExternalStorageDirectory();

   String path=file.getAbsolutePath();

判断sd卡是否可用

Environment..MEDIA_MOUNTED.equals(Environment.getExternalStorageState());

获得sd卡的可用空间

file.getFreeSpace();


 

更多相关文章

  1. Android中不同应用间实现SharedPreferences数据共享
  2. 关于Android(安卓)Studio3.2新建项目Android(安卓)resource link
  3. Android(安卓)- Manifest 文件 详解
  4. Android之应用程序基础
  5. Android四大组件的理解
  6. Android官方入门文档[1]创建一个Android项目
  7. 第三章 Android程序设计基础
  8. 第一章 andriod studio 安装与环境搭建
  9. Android(安卓)Audio Focus的应用(requestAudioFocus)

随机推荐

  1. Android(安卓)Studio中统一管理版本号引
  2. Android注册界面设计
  3. Ubuntu下为AndroidStudio编译并使用x264(
  4. Android(安卓)的回调事件详解
  5. Android(安卓)HAL 开发 (4)
  6. android manager 更新 android的有效方法
  7. 设置android软键盘,默认不弹出的方法
  8. Android(安卓)学习(1)——AndroidStudio安
  9. android LayoutInflater的使用
  10. Android(安卓)学习记录-获取天气API