温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

数据存储(持久化)

发布时间:2020-07-05 19:36:26 来源:网络 阅读:333 作者:yahaa 栏目:移动开发

14.3.01


数据存储(持久化)  

五种:(外存)

1、Shared Preferences

Store private primitive data in key-value pairs.

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

2、Internal Storage

Store private data on the device memory.

内部存储

3、External Storage

Store public data on the shared external storage.

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

4、SQLite Databases

Store structured data in a private database.

数据库存储(外存)

5、Network Connection

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<span style=\"\\"font-size:14px;font-family:'times\">”,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());

获得

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI