内部储存
//使用sharedPReference来保存用户名和密码//路径在data/data/com.duyi.sharedpreference/share_prefsSharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);//拿到sp的编辑器Editor ed = sp.edit();ed.putString("name", name);ed.putString("pass", pass);//提交ed.commit()
获取;
SharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);String name = sp.getString("name", "");String pass = sp.getString("pass", "");
外部储存
获得外部存储的路径Environment.getExternalStorageDirectory()
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
检测外部存储的状态//MEDIA_UNKNOWN:不能识别sd卡。//MEDIA_REMOVED:没有sd卡。//MEDIA_UNMOUNTED:sd卡存在,但是没有挂载。4.0之前,有卸载选项。现在已经没有了。//MEDIA_CHECKING:sd卡正在准备。//MEDIA_MOUNTED:sd卡已经挂载,可用。
获得sd卡剩余容量
File path = Environment.getExternalStorageDirectory();StatFs stat = new StatFs(path.getPath());long blockSize = stat.getBlockSize();//总块数long totalBlocks = stat.getBlockCount();//可用的块儿数long availableBlocks = stat.getAvailableBlocks();
文件流储存
FileOutputStream fs= null; try { fs=openFileOutput("count", Context.MODE_PRIVATE+Context.MODE_APPEND); String str=name.getText()+","+age.getText()+","+ophone.getText(); fs.write(str.getBytes()); fs.flush(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { fs.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
读取
FileInputStream fs=null; try { fs=openFileInput("count"); byte[] b=new byte[1024]; int len; StringBuilder sb=new StringBuilder(); while((len=fs.read(b))!=-1){ sb.append(new String(b, 0,len)); } tv.setText(sb.toString()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if (fs!=null) { try { fs.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
新闻热点
疑难解答