android学习----主要数据存储方

一 (放到内存).//文件读取  /data/data/package(包名)/file  

//数据存入File文件   
FileOutputStream outStream= this.openFileOutput("text.text", MODE_PRIVATE);
outStream.write("hello".getBytes());
outStream.close();
//file取数据
FileInputStream inStream =this.openFileInput("text.text");
ByteArrayOutputStream bOutStream=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int length=0;
while((length=inStream.read(buffer))!=-1){
bOutStream.write(buffer, 0, length);
}
inStream.close();
bOutStream.close();

bOutStream.toString();//结构

//end

补充

this.getFilesDir(); // /data/data/包名/files
this.getCacheDir(); // /data/data/包名/cache

   2.把文件存放到SDCard 上 /mnt/sdcard/ (这个是模拟器的路径)

   1)先申请SD权限android.permission.mount_UnMount_FILESYSTEMS    以及读写权限 android.permission.WRITE_EXTERNAL_STORAGE

//把文件放到SD code
if(Environment.getExternalStorageState()==Environment.MEDIA_MOUNTED){//有SD卡
File externalStorageDirectory = Environment.getExternalStorageDirectory();// /mnt/sdcard/
FileOutputStream out=new FileOutputStream(externalStorageDirectory);//推荐这样获取
out.write("hello".getBytes());
out.close();


二、sharedpreferences 存储数据  存放类型 XML 文件存放在/data/data/<package name>/shared_prefs目录下

   SharedPreferences sp=getSharedPreferences("abc", MODE_PRIVATE);
Editor edit = sp.edit();
edit.putBoolean("a", true);
edit.putInt("b", 1);
edit.putString("c", "abc");
edit.commit();

三、SQLite数据库 (暂略) 

四、Content provider   内容提供者(暂略)

  

          


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值