Android -- Android数据存储
⒈SharedPreferences轻量级数据存储【通常用来存储例如App的一些设置信息、用户名密码等】
- Xml文件,K-V形式
- SharedPreferences,通过它可以完成对数据读的操作。
- SharedPreferences.Editor,通过它可以完成对数据写的操作。
1.文件目录
/data/data/<applicationId>/shared_prefs
**包名和applicationId不一样
applicationId可以修改
Android Studio =》 View=》 Tool Windows =》 Device File Explorer
2.使用方式
//数据存储 SharedPreferences sharedPreferences = null; SharedPreferences.Editor editor = null; //第一个参数为文件名 //第二个参数为模式 //MODE_PRIVATE --- 这个文件只有当前应用可以进行读写,其他App无法读写,也获取不到【常用】 //MODE_WORLD_READABLE --- 其他的应用可以读取 //MODE_WORLD_WRITEABLE --- 其他的应用可以写 //MODE_APPEND --- 在文件追加写入,不会覆盖文件 sharedPreferences = getSharedPreferences("data",MODE_PRIVATE); editor = sharedPreferences.edit(); editor.putString("name","fanqi"); //commit是一个同步存储的过程,会阻塞线程,apply是一个异步的过程,会在后台进行,推荐apply editor.apply(); //需要提交后才能生效 //editor.commit(); //读取数据 //参数值为key和缺省值,如果没有取到则返回缺省值 sharedPreferences.getString("name","");
⒉Android存储概念
分类
-
- 内部存储【Internal Storage】,随应用卸载而被删除
- /data/data/<applicationId>/shared_prefs
- /data/data/<applicationId>/databases
- /data/data/<applicationId>/files【context.getFilesDir()】
- /data/data/<applicationId>/cache【context.geteCacheDir()】
- 外部存储【External Storage】
- 共有目录【Environment.getExternalStoragePublicDirectory(int type)】
- 私有目录【Android】,随应用卸载而被删除
- /mnt/sdcard/Android/data/data/<applicationId>/cache【】
- /mnt/sdcard/Android/data/data/<applicationId>/files【】
- 内部存储【Internal Storage】,随应用卸载而被删除
⒊File内部存储
- 利用Java的I/O流
- FileOutputStream FileInputStream
private void save(String content){ FileOutputStream fileOutputStream = null; try { fileOutputStream = openFileOutput("test.txt",MODE_PRIVATE); fileOutputStream.write(content.getBytes()); } catch (IOException e) { e.printStackTrace(); }finally { if(fileOutputStream != null){ try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } private String read(){ FileInputStream fileInputStream = null; try { fileInputStream = openFileInput("test.txt"); byte[] buff = new byte[1024]; StringBuilder builder = new StringBuilder(); int len = 0; while ((len = fileInputStream.read(buff)) > 0){ builder.append(new String(buff,0,len)); } return builder.toString(); } catch (IOException e) { e.printStackTrace(); }finally { if(fileInputStream != null){ try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }
⒋File外部存储
private void save(String content){ FileOutputStream fileOutputStream = null; try { //fileOutputStream = openFileOutput("test.txt",MODE_PRIVATE); File dir = new File(getExternalFilesDir(null),"coreqi"); if(!dir.exists()){ dir.mkdir(); } File file = new File(dir,"test.txt"); if(!file.exists()){ file.createNewFile(); } fileOutputStream = new FileOutputStream(file); fileOutputStream.write(content.getBytes()); } catch (IOException e) { e.printStackTrace(); }finally { if(fileOutputStream != null){ try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } private String read(){ FileInputStream fileInputStream = null; try { //fileInputStream = openFileInput("test.txt"); File file = new File(getExternalFilesDir(null).getAbsolutePath() + File.separator + "coreqi","test.txt"); fileInputStream = new FileInputStream(file); byte[] buff = new byte[1024]; StringBuilder builder = new StringBuilder(); int len = 0; while ((len = fileInputStream.read(buff)) > 0){ builder.append(new String(buff,0,len)); } return builder.toString(); } catch (IOException e) { e.printStackTrace(); }finally { if(fileInputStream != null){ try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }
在AndroidManifest.xml中声明SD卡权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
**Android6.0之后的权限需要动态的获取,即如果你的build.gradle中compileSdkVersion的版本是23或23以上的,那么你需要考虑6.0以上动态权限的问题。
**动态申请权限
//字符串为要申请权限的数组 ActivityCompat.requestPermissions(this,new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE },1);
作者:奇
出处:https://www.cnblogs.com/fanqisoft/p/12171324.html
版权:本作品采用「本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。」许可协议进行许可。
分类:
Android
如果文章内容对您有所帮助,欢迎赞赏.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2019-01-09 Spring 基于注解的AOP实现