1.SharedPreferences
a)存储数据 ( 注意:会自动生成xml文件,所有不要写后缀名)
SharedPreferences share = getSharedPreferences(" zhou", MODE_PRIVATE ); //参数为文件名和权限Editor ed = share.edit(); //拿到share的编辑器ed.putString("name", String str); //加入数据,以键值对加入,也可以加入其它类型的数据,这里是Stringedit.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(
内部存储)
3.ExternalStorage (外部存储(SD卡))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();
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();}