存储SharedPreferences、InternalStorage、ExternalStorage

本文详细介绍了Android应用中常见的四种数据存储方式:SharedPreferences、Internal Storage、External Storage及使用方法。包括如何通过SharedPreferences进行简单的键值对存储,Internal Storage中利用文件流进行数据读写,以及在External Storage中进行文件操作的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.SharedPreferences
a)存储数据  (注意:会自动生成xml文件,所有不要写后缀名
SharedPreferences share = getSharedPreferences("zhou", MODE_PRIVATE   );//参数为文件名和权限
   Editor ed = share.edit();//拿到share的编辑器
   ed.putString("name", String str);  //加入数据,以键值对加入,也可以加入其它类型的数据,这里是String
  edit.putInt(key, value);
   ed.commit();//提交
b)取出数据
SharedPreferences s = getSharedPreferences("zhou", MODE_PRIVATE );  //第一个参数,对应的文件名
  String str = s.getString("name", "");//第一参数是key,第二个参数是如果没取到的默认值
  String str1 = s.getString("pass", "");
2.InternalStorage内部存储)
1)openFileOutput方法
//保存数据
FileOutputStream fos = context.openFileOutput(String fileName,Context.MODE_PRIVATE  /*这里一般是默认写这个(私有)*/   );  //1.获取输出流
fos.write(byte[] b); //2.写数据
fos.flush();//2.关流
//取出数据
FileInputStream fis = context.openFileInput(String fileName);//1.得到输出流 
ByteArrayOutputStream bos = new ByteArrayOutputStream();//2.接收流 
//3..读数据
   byte[] b = new byte[1024];
   int len = 0;
   while ((len = fis.read(b)) != -1) {
    bos.write(b, 0, len);
    bos.flush();
    }
bos.toByteArray();//返回东西,这里是byte[]类型
2)getCacheDir()方法
//保存数据
File cacheDir = context.getCacheDir(); //1.得到cache目录
File file = new File(cacheDir, fileName);//2.创建路径
FileOutputStream fos = new FileOutputStream(file); //3.创建输出流
//4.写,写完刷新输出流
fos.write(b);
fos.flush();
//取出数据
  ByteArrayOutputStream bos = new ByteArrayOutputStream(); //1.接收数据的流 
  File cacheDir = context.getCacheDir()//2.等到cache的路径,加上文件名 
  File file = new File(cacheDir, fileName);
  //3.严谨,判断是否存在
  if (!file.exists()) {   System.out.println("+++++++==文件不存在");   }
//4.得到输入流,数据缓存到接收流中
   FileInputStream fis = new FileInputStream(file);
   byte[] buff = new byte[1024];
   int len;
   while ((len = fis.read(buff)) != -1) {
    bos.write(buff, 0, len);
    bos.flush();
   }
   //5.返回值
 bos.toByteArray();
3.ExternalStorage (外部存储(SD卡))
a )判断sd卡是否挂载上,并且可读可写
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED );
b )获得SD卡的根目录 
注意了:一般来说SdCard的根目录是/mnt/sdcard,但是有些手机的SdCard的根目录:/storage/sdcard/
Environment.getExternalStorageDirectory().getAbsolutePath();
c )保存数据到根目录
//1.判断是否挂载SD卡,并可读可写
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED )) {
//2.得到SD卡的根目录,加上要存的文件名字
File dir = Environment.getExternalStorageDirectory();
File file = new File(dir, fileName);
//3.输出流,写
FileOutputStream fos = new FileOutputStream(file);
fos.write(b);
fos.flush();
}
d )保存数据到自定义目录
//1.判断是否挂载SD卡,并可读可写
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED )) {
//2.得到SD卡的根目录,加上要自定义文件名
File dir = Environment.getExternalStorageDirectory();
File filedir = new File(dir, newfile); //这里添加一个自定义目录
if(!filedir.exists() ){
file.madirs();//如果文件不存在,创建
}
//3.加上文件名字
File file = new File(filedir , fileName);
//4.输出流,写
FileOutputStream fos = new FileOutputStream(file);
fos.write(b);
fos.flush();
}
e )保存数据到公共(public)目录
//得到SD卡的公共目录
File publicDir = Environment.getExternalStoragePublicDirectory(type);
type 里面有:Environment.DIRECTORY_PICTURES  等等。


d)从SD卡中取出数据

//ScCard根目录  String path = Environment.getExternalStorageDirectory()+"/文件名";
//SdCard自定义目录 String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/自定义文件名"+"/文件名";
//SdCatd公共目录 String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_* /*公共文件名*/  ).getAbsolutePath()+"/文件名";

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED )){
//1.要取出数据的路径
File file = new File (path);
//2.接收流
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//3.输出流,输出并接收
FileInputStream fis = new FileInputStream(file);
byte[] b = new byte[1024];
int len = 0;
while((len = fis.read(b)) !=- 1){
bos.write(b, 0, len);
//4.返回值
bos.toByteArray();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值