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

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