Android存取数据的几种方式

来源:互联网 发布:淘宝手机端设为首页 编辑:程序博客网 时间:2024/05/01 15:56

一:自定义一个路径存储数据

/** * 自定义一个路径存储数据 这里自定义一个/data/data/com.yin.qqLogin/msg.txt路径 * 发现一个有意思的现象,不能用$分割字符串,不然取不到,不知为什么 * @author Administrator * */public class Utils {/** * 根据绝对路径保存qq号和密码 * @return */public static boolean saveUserInfo(String number,String password){//绝对路径String path="/data/data/com.yin.qqLogin/msg.txt";String msg=number+"$$"+password;File file=new File(path);try {FileOutputStream fos=new FileOutputStream(file);//也可以直接传入pathfos.write(msg.getBytes());fos.flush();fos.close();} catch (Exception e) {e.printStackTrace();return false;}return true;}/** * 根据绝对路径获取qq号和密码 */public static Map<String, String> getUserInfo(){Map<String,String> map=null;try {String path="/data/data/com.yin.qqLogin/msg.txt";File file=new File(path);if(!file.exists()){return null;}/** * FileInputStream:字节流 * InputStreamReader:字节流转换为字符流的桥梁 * BufferedReader:字符缓冲流,可实现搞笑读取,即一次可以读取很多 */FileInputStream fis=new FileInputStream(file);//也可以直接传入pathBufferedReader bf=new BufferedReader(new InputStreamReader(fis));String data=bf.readLine();if(!TextUtils.isEmpty(data)){String[] info=data.split("$$");map=new HashMap<String, String>();map.put("number", info[0]);map.put("password", info[1]);}bf.close();} catch (Exception e) {e.printStackTrace();return null;}return map;}}

二:在/data/data/包名/filses或/data/data包名/cache路径下存储数据

/** * 在/data/data/当前包名/files目录下存储数据 * 或者在/data/data/当前包名/cache目录下存储数据 * @author Administrator * */public class Utils {/** * 自动获取当前应用的在手机内存中的数据局存储路径:Context.getFileDir(); * 返回的是/data/data/当前包名/files */public static boolean saveUserInfo(Context context,String number,String password){File fileDir=context.getFilesDir();/** * 手机中,设置>应用程序>管理应用程序>选择一个应用>会看到clean data,其中就有cache *///File fileDir=context.getCacheDir();//获取缓存目录File file=new File(fileDir, "info.txt");try {FileOutputStream fos=new FileOutputStream(file);String info=number+"##"+password;fos.write(info.getBytes());fos.flush();fos.close();return true;} catch (Exception e) {e.printStackTrace();}return false;}/** * 自动获取当前应用的在手机内存中的数据局存储路径:Context.getFileDir(); * 返回的是/data/data/当前包名/files */public static Map<String,String> getUserInfo(Context context){File fileDir=context.getFilesDir();//File fileDir=context.getCacheDir();//获取缓存目录File file=new File(fileDir,"info.txt");try {FileInputStream fis=new FileInputStream(file);BufferedReader bf=new BufferedReader(new InputStreamReader(fis));String text=bf.readLine();if(!TextUtils.isEmpty(text)) {String[] split = text.split("##");Map<String, String> userInfoMap = new HashMap<String, String>();userInfoMap.put("number", split[0]);userInfoMap.put("password", split[1]);return userInfoMap;}} catch (Exception e) {e.printStackTrace();}return null;}}

三:通过SDCard存取数据

public class UtilsOfSDCard {/** * 向sd卡中存入数据 */public static boolean saveUserInfo(String number,String password){//判断设备是否已经安装了sd卡String status=Environment.getExternalStorageState();//如果没有sd则返回falseif(!Environment.MEDIA_MOUNTED.equals(status)){return false;}//如果已经安装了sd卡File file=new File(Environment.getExternalStorageDirectory(),"info.txt");try {FileOutputStream fos=new FileOutputStream(file);String data=number+"##"+password;fos.write(data.getBytes());fos.flush();fos.close();return true;} catch (Exception e) {e.printStackTrace();}return false;}/** * 从sd卡读取数据 */public static Map<String,String> getUserInfo(){//判断设备是否装了sd卡String status=Environment.getExternalStorageState();if(!Environment.MEDIA_MOUNTED.equals(status)){return null;}//如果装了sd卡,则从sd卡中取出数据File file=new File(Environment.getExternalStorageDirectory(),"info.txt");try {FileInputStream fis=new FileInputStream(file);BufferedReader bf=new BufferedReader(new InputStreamReader(fis));String info=bf.readLine();if(!TextUtils.isEmpty(info)){Map<String,String> map=new HashMap<String, String>();String[] data=info.split("##");map.put("number", data[0]);map.put("password", data[1]);bf.close();return map;}} catch (Exception e) {e.printStackTrace();}return null;}}

四:通过SharedPreferences存取数据

public class UtilsOfShared {/** * 用SharedPreferences保存用户信息 */public static boolean saveUserInfo(Context context,String number,String password){/** * SharedPreferences: * 创建/data/data/包名/shared_prefs/info.xml, * 即使你自己定义了文件类型,创建的结果仍然是xml文件, * 例如你要创建info.txt,实际创建的结果是info.txt.xml */try {SharedPreferences sp=context.getSharedPreferences("info", Context.MODE_PRIVATE);//获取一个编辑器Editor editor=sp.edit();//用编辑器将number和password放入info.xml中editor.putString("number", number);editor.putString("password", password);//至此,编辑器真正提交数据editor.commit();return true;} catch (Exception e) {e.printStackTrace();return false;}}/** * 用SharedPreferences获取用户信息 */public static Map<String,String> getUserInfo(Context context){//创建一个SharedPreferencesSharedPreferences sp=context.getSharedPreferences("info", Context.MODE_PRIVATE);//从sp中获取number和passwordString number=sp.getString("number", null);String password=sp.getString("password", null);//判断number,password是否为nullif(!TextUtils.isEmpty(number)&&!TextUtils.isEmpty(password)){Map<String ,String> map=new HashMap<String, String>();map.put("number", number);map.put("password", password);return map;}return null;}}

总结:

通过以上几种方式存储的数据,在app卸载后,你在SDCard中创建的目录存储的数据不会被删,但是其它几种方式存储的数据都会被删除,即在/data/data/包名下的数据都会被删除。


0 0
原创粉丝点击