Android数据存储与IO

来源:互联网 发布:qq三国100js单刷过关 编辑:程序博客网 时间:2024/05/18 00:16

Android数据本地存储大致有四种:直接通过IO流进行存储到本地,通过SharedPreferences存储到本地,存储到SD卡,通过SQLite数据库进行存储。

1,直接通过IO流进行存储到本地。

这种方法与java中的存储类似,这里直接给上示例代码:

存储到本地:

public static boolean saveUserInfo(Context context,String number,String password){       try {            //String path = "/data/data/com.jinglion.qq/jingqq.txt";            File filesDir = context.getFilesDir();            File f = new File(filesDir, "jingqq.txt");            FileOutputStream fos = new FileOutputStream(f);            String data = number+"##"+password;            fos.write(data.getBytes());            fos.flush();            fos.close();            return true;        } catch (IOException e) {            e.printStackTrace();        }        return false;}
        上述代码实例中通过context.getFilesDir()获取每个应用的路径,确保写入文件的路径的唯一性,通过new File(Path , filename)来在指定位置创建一个file对象。该实例使用FileOutputStream字节流写入到本地。
从本地读取数据:

public static Map<String, String> getUserInfo(Context context){try {//String path = "/data/data/com.jinglion.qq/jingqq.txt";File filesDir = context.getFilesDir();File f = new File(filesDir, "jingqq.txt");FileInputStream fis = new FileInputStream(f);BufferedReader bufr = new BufferedReader(new InputStreamReader(fis));String text = bufr.readLine();if(!TextUtils.isEmpty(text)){String[] sp = text.split("##");Map<String, String> userInfoMap = new HashMap<String, String>();userInfoMap.put("number", sp[0]);userInfoMap.put("password", sp[1]);return userInfoMap;}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}
        从本地读取数据采用了FileInputStream字节流方法,并使用了字符流BufferedReader方法来进行读取,因为字符流具有readline()方法,该方法能够一次读取一整行,有效的提高读取效率。从字节流到字符流的转换需要使用转换流InputStreamReader类。

        该实例实现了字节流到字符流的转换:

    BufferedReader bufr = new BufferedReader(new InputStreamReader(fis));
        读取到数据后,通过split方法完成分割,用Map存储相应数据。

2,通过SharedPreferences存储到本地

该方法与直接通过IO流实现数据的存储其实十分类似,只是通过编辑器对象来进行存储。

存储实例:

public static boolean saveUserInfo(Context context,String number,String password){try {SharedPreferences sp = context.getSharedPreferences("jinglionqq", Context.MODE_PRIVATE);//获得一个编辑器对象Editor edit = sp.edit();//存储数据edit.putString("number", number);edit.putString("password", password);//提交,数据真正存储起来edit.commit();return true;} catch (Exception e) {e.printStackTrace();}return false;}
该方法通过Editor edit = sp.edit();来获得编辑器对象,并使用putString(key, value)方法来存储数据,值得注意的是,真正把数据存储起来是通过commit()方法,这一点与字符流里面的flush()方法类似。

读取实例:

public static Map<String, String> getUserInfo(Context context){try {SharedPreferences sp = context.getSharedPreferences("jinglionqq", Context.MODE_PRIVATE);String number = sp.getString("number", null);String password = sp.getString("password", null);if (!TextUtils.isEmpty(number) && !TextUtils.isEmpty(password)) {Map<String, String> usersInfoMap = new HashMap<String, String>();usersInfoMap.put("number", number);usersInfoMap.put("password", password);return usersInfoMap;}} catch (Exception e) {e.printStackTrace();}return null;


3,存储到SD卡

存储到SD卡也是直接使用了java的IO流操作,只有两点不同

                   1,通过Environment.getExternalStorageDirectory()方法获取SD卡的路径。

                   2,必须添加访问和操作SD卡的权限:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

存储实例:

public static boolean saveUserInfo(Context context,String number,String password){FileOutputStream fos = null;try {File filesSDCard = Environment.getExternalStorageDirectory();File f = new File(filesSDCard, "jingqq.txt");fos =new FileOutputStream(f);String data = number+"##"+password;fos.write(data.getBytes());return true;} catch (IOException e) {e.printStackTrace();} finally{if (fos!=null) {try {fos.close();} catch (IOException e) {e.printStackTrace();}}}return false;}

读取实例:

public static Map<String, String> getUserInfo(Context context){try {File filesSDCard = Environment.getExternalStorageDirectory();File f = new File(filesSDCard, "jingqq.txt");BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileInputStream(f)));String text = bufr.readLine();bufr.close();if (!TextUtils.isEmpty(text)) {String[] sp = text.split("##");Map<String, String> usersInfoMap = new HashMap<String, String>();usersInfoMap.put("number", sp[0]);usersInfoMap.put("password", sp[1]);return usersInfoMap;}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return null;}



0 0
原创粉丝点击