Android的4种数据存储方法

Android的4种数据存储方法

一.保存到私有目录文件

  • 无权限模式

    File file = new File(context.getFilesDir(),"文件名.扩展名");
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    fileOutputStream.write(文件名.getBytes());
    fileOutputStream.close();
    
  • 有权限模式

    FileOutputStream fileOutputStream =context.openFileOutput("文件名.扩展名",Context.权限模式);
    fileOutputStream.write(文件名.getBytes());
    fileOutputStream.close();
    FileInputStream fileInputStream = context.openFileOutput("文件名.扩展名");
    
  • 读取文件

    无权限模式

    File file = new File (context.getFilesDir(),”文件名.扩展名”);
    FileInputStream fis = new FileInputStream(file);
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));

    有权限模式

    FileInputStream fis = context.openFileInput("文件名.扩展名");
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
    

注意:☆☆☆关闭输入输出流

实例源码http://pan.baidu.com/s/1kVJKPFp
http://pan.baidu.com/s/1cyy42M

二.使用SharePerence存储

  • SharePreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
    editor.putString(String KEY,name);
    editor.commit();
    
  • SharePreferences myPref = getPreferences(MODE_PRIVATE);
    String str = myPref.getString(String KEY,"不存在返回的数值");
    

三.使用SD卡存储

  • //  String path = "/mnt/sdcard/";
    //指定保存的路径
    //通过Environment获取sdcard的目录
    String path = Environment.getExternalStorageDirectory().getPath();
    File file = new File(path,"文件名.扩展名");//创建file
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    fileOutputStream.close();
    

四.SQLite存储

  • 新建继承SQLiteOpenHelper类

    public class MySQlite extends SQLiteOpenHelper {
    private Context mContext;
    //定义一个名为Person的表
    public static final String CREATE_PERSON = "create table Person ("
        + "id integer primary key autoincrement, "      
        + "ages integer, "
        + "name text)";
    // 为表里添加phone数据
    public static final String UPDATE_BOOK = "alter table BookStore add phone integer(11)";
    
    public MySQlite(Context context) {
    super(context,"BookStore.db", null,1);
    mContext = context;
    }
    
    public void onCreate(SQLiteDatabase db) {
    // 创建表
    db.execSQL(CREATE_BOOK);
    //添加数据
    db.execSQL(UPDATE_BOOK);
    }
    
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // 添加没有break的switch的语句可以使后版本才下载的客户可使用以前的数据结构    
    db.execSQL(CREATE_PERSON);
     }
    }
    
  • 提取数据库中数据

    使用SQLite的query方法

    public ArrayList query(String name){
    ArrayList list = new ArrayList();
    private MySqliteOpenHelper mySqliteOpenHelper;
    SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
    Cursor cursor = db.query(“info”, new String[]{“_id”,”name”,”phone”}, “name = ?”, new String[]{name}, null, null, “_id desc”);
    //解析Cursor中的数据
    if(cursor != null && cursor.getCount() >0){//判断cursor中是否存在数据

        //循环遍历结果集,获取每一行的内容
        while(cursor.moveToNext()){//条件,游标能否定位到下一行
            InfoBean bean= new InfoBean();
            //往javabean中获取数据
            bean. id = cursor.getInt(0)+"";
            bean. name = cursor.getString(1);
            bean. phone = cursor.getString(2);
            list.add(bean);     
        }
     //关闭游标和数据库
        cursor.close();
       }
        db.close();
      return list;
     }
    

实例源码 http://pan.baidu.com/s/1c12NSyc

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章