day4.13总结_SharedPreferences和IO存储和SQLite

一、Data Store (数据存储)

 

(1)External Storeage(外部存储):外置sdcard

1)直接IO存储

a)存储状态(Environment.getExternalStorageState())

b)存储目录(Environment.getExternalStorageDirectory()

c)存储大小 StatFs sf=new StatFs(sdcard.getPath());

d)存储权限 android.permission.WRITE_EXTERNAL_STORAGE

 

 

说明:如何获得应用程序外置sdcard私有存储目录,

此目录中的数据有何特点?

a)getExternalCacheDir()

例子1:外部存储到外置sdcard

public void onClick(View v){

//获得res/raw目录的a文件的输入流

InputStream in=getResources().openRawResource(R.raw.a);

//a写到外置sdcard

//1.外置sdcard的存储状态

String state=Environment.getExternalStorageState();

if(!state.equals(Environment.MEDIA_MOUNTED)){//mounted

Toast.makeText(this,"请确保sdcard已链接", 1).show();

return;

}

//2.外置sdcard的存储目录大小

//应用程序公有存储目录

//File picDir=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

//应用程序在sdcard的私有存储目录(数据在应用卸载时会被删除)

File picDir=getExternalFilesDir(Environment.DIRECTORY_PICTURES);

//File picDir=getExternalCacheDir();

StatFs sf=new StatFs(picDir.getPath()); 借助此对象获得磁盘的存储信息

int aSize=sf.getAvailableBlocks()*sf.getBlockSize();

FileOutputStream out=null;

byte[]buf=new byte[1024];

int len=-1;

try{

if(aSize<in.available()){

Toast.makeText(this, "空间不足", 1).show();

return;

}

//3.假如有空间则将a文件写入到sdcardpictures目录

out=new FileOutputStream(new File(picDir,"a.jpg"));

while((len=in.read(buf))!=-1){

out.write(buf, 0, len);

}

Toast.makeText(this, "写入ok!", 1).show();

}catch(Exception e){

e.printStackTrace();

}finally{

if(in!=null)try{in.close();in=null;}catch(Exception e){}

if(out!=null)try{out.close();out=null;}catch(Exception e){}

}

}

 

b)getExternalFilesDir(String type)

2.Internal storeage (内部存储):内置sdcard

1)直接IO存储 (data/data/项目包/files):私有,应用卸载数据会被删除

a)openFileInput

b)openFileOutput

说明:直接IO存储时缓存目录的获得:getCacheDir()

例子2内部存储到内置sdcard

//data/data/项目包/files

private void ioStore01()throws Exception{

  FileOutputStream out=openFileOutput("a1.txt", Context.MODE_PRIVATE);//Context.MODE_APPEND 存放 //data/data/项目包/files   out.write("helloworld".getBytes());

  out.close();

  //==========

  byte buf[]=new byte[10];

  FileInputStream in=openFileInput("a1.txt");

  in.read(buf);

  Log.i("TAG",Arrays.toString(buf));

  Log.i("TAG",new String(buf));

  in.close();

}

private void ioStore02()throws Exception{   存放//data/data/项目包/cache

File cacheDir=getCacheDir();

Log.i("TAG",

"cacheDir="+cacheDir.getPath());

OutputStream out=new FileOutputStream(new File(cacheDir,"a1.txt"));

out.write("helloworld".getBytes());

out.close();

Log.i("TAG", "cache write ok!");

}

 

2)偏好存储(SharedPreferences):记录用户喜好

a)SharedPreferences (data/data/项目/share_prefs/XXX.xml)

b)Editor(存储key/value)

扩展:系统设置实现(参考uicommons/settings)

例子3偏好存储(SharedPreferences):

private void shareStore01(){

//获得SharedPreferences对象

SharedPreferences sp=getSharedPreferences("s1",Context.MODE_PRIVATE);

//获得Editor对象

Editor et=sp.edit();//key/value

//将数据存储到Editor对象

et.putString("phone","139");

et.putBoolean("state",true);

//Editor对象的数据写入到文件     share_pref文件夹

et.commit();

}

private void shareStore02(){

//获得SharedPreferences对象

SharedPreferences sp=getSharedPreferences("s1",Context.MODE_PRIVATE);

//读数据

String phone=sp.getString("phone","");//第二个参数为没找到值时的默认值

boolean state=sp.getBoolean("state", false);

//输出

Log.i("TAG", "phone="+phone);

Log.i("TAG", "state="+state);

}

 

(3)SQLite存储

a)SQLite 是一个开源,轻量级关系型数据库系统,底层使用c/c++实现。

b)SQLite 一般应用于便携式设备,无需启动服务。

c)Adnroid 内置了SQLite 数据库,可以存储联系人信息,媒体库信息,备忘录信息,。。。

d)Android SQLite 相关 APISQliteDatabase,....

c)SQLite SQL的应用? (DDL,DML,DCL)

 

SQLiteDatabase的常用方法 :

打开或创建数据库

openOrCreateDatabase(String path,SQLiteDatabase.CursorFactory  factory)

 

插入一条记录

insert(String table,String nullColumnHack,ContentValues  values)

 

删除一条记录

delete(String table,String whereClause,String[]  whereArgs)

 

查询一条记录

query(String table,String[] columns,String selection,String[]  selectionArgs,String groupBy,String having,String  orderBy)

 

修改记录

update(String table,ContentValues values,String whereClause,String[]  whereArgs)

 

执行一条SQL语句

execSQL(String sql)

 

关闭数据库

close()

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值