android持久化存储,数据存储(持久化)

14.3.01

数据存储(持久化)

五种:(外存)

Store private primitive data in key-value pairs.

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

Store private data on the device memory.

内部存储

Store public data on the shared external storage.

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

Store structured data in a private database.

数据库存储(外存)

Store data on the web with your own network server.

网络存储

1、sharePreferences

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

例子:按钮点击保存

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

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

//编辑器类型的对象

Editor edit=Preference.edit();

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

Edit.putBoolean(“name”);

//提交

Commit();

获取数据

getBoolean(“name”);//SS:默认值

SharedPreferences preferences = getPreferences(0);

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

}

保存位置

Data/data/包名“xibo”,0);//xibo保存的文件名

2、InternalStorage

保存位置:

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

保存

FileOutputStream openFileOutput =null;

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

openFileOutput.write(str.getBytes());

e.printStackTrace();

e.printStackTrace();

if(openFileOutput!=null){

openFileOutput.close();

e.printStackTrace();

}

}

读取

FileInputStream

FileInputStream fis=null;

try {

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

byte[] buffer=newbyte[1024];

bab.append(buffer, 0, len);

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

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

e.printStackTrace();

e.printStackTrace();

if(fis!=null){

fis.close();

e.printStackTrace();

}

}

读写模式:

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

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

MODE_WORLD_READABLE其他应用可读

MODE_WORLD_WRITEABLE其他应用可写

3、外部读取

查找sd卡是否可用

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

获得

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值