成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

Android持久化存儲(chǔ)總結(jié)

Eric / 2082人閱讀

摘要:本文旨在復(fù)習(xí)總結(jié)持久化存儲(chǔ)方法,只列出了主要代碼以及少量批注,僅作為復(fù)習(xí)參考使用。

本文旨在復(fù)習(xí)總結(jié)Android持久化存儲(chǔ)方法,只列出了主要代碼以及少量批注,僅作為復(fù)習(xí)參考使用。

文件存儲(chǔ)

所有文件默認(rèn)放在/data/data//files/目錄下

文件寫入
    public void save(String inputText){
        FileOutputStream out=null;
        BufferedWriter writer=null;
        try {
            out=openFileOutput("data", Context.MODE_PRIVATE);
            writer=new BufferedWriter(new OutputStreamWriter(out));
            writer.write(inputText);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
讀取文件
    public String load(){
        FileInputStream in =null;
        BufferedReader reader=null;
        StringBuilder content=new StringBuilder();
        try {
            in=openFileInput("data");
            reader=new BufferedReader(new InputStreamReader(in));
            String line="";
            while ((line=reader.readLine())!=null){
                content.append(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return content.toString();
    }
SharePreferences方法

SharePreference文件默認(rèn)放在/data/data//shared_prefs/目錄下

SharedPreferences寫入
SharedPreferences.Editor editor=getSharedPreferences("data",MODE_PRIVATE).edit();
editor.putString("et_inputText","sharePreferences test");
editor.commit();
SharedPreferences讀取
SharedPreferences sp=getSharedPreferences("data",MODE_PRIVATE);
String input=sp.getString("et_inputText","請(qǐng)輸入用戶名");//第二個(gè)參數(shù)是為空的默認(rèn)信息
SQLite數(shù)據(jù)庫(kù)存儲(chǔ)

SQLite文件默認(rèn)放在/data/data//databases/目錄下

public class MyDataBaseHelper extends SQLiteOpenHelper{
    private static final String CREATE_BOOK="create table Book("
            +"id integer primary key autoincrement, "
            +"author text, "
            +"price real, "
            +"pages integer, "
            +"name text)";
    private static final String CREATE_CATEGORY="create table Category("
            +"id integer primary key autoincrement, "
            +"category_name text, "
            +"category_code integer)";
    private Context mContext;
    public MyDataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        mContext=context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_BOOK);
        db.execSQL(CREATE_CATEGORY);
        Toast.makeText(mContext,"create succeed",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists Book");
        db.execSQL("drop table if exists Category");
        onCreate(db);
    }

創(chuàng)建數(shù)據(jù)庫(kù)

 dbHelper=new MyDataBaseHelper(this,"BookStore.db",null,2);

插入數(shù)據(jù)

SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name","Effective Java");
values.put("author","Joshua Bloch");
values.put("pages",454);
values.put("price",16.96);
db.insert("Book",null,values);

更新數(shù)據(jù)

ContentValues values=new ContentValues();
values.put("price",198.00);
SQLiteDatabase db=dbHelper.getReadableDatabase();
db.update("Book",values,"name=?",new String[]{"Android Programme"});

刪除行

SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("Book", "pages > ?", new String[] { "500" });

ContentValues知識(shí)android提供的一個(gè)便于數(shù)據(jù)庫(kù)操作的類,為數(shù)據(jù)庫(kù)操作提供便利,也可以直接使用數(shù)據(jù)庫(kù)語句進(jìn)行操作

參考書目《第一行代碼》

更多關(guān)于java的文章請(qǐng)戳這里:(您的留言意見是對(duì)我最大的支持)

我的文章列表
Email:sxh13208803520@gmail.com

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/66961.html

相關(guān)文章

  • 01.Android之基礎(chǔ)組件問題

    摘要:此時(shí)再次旋轉(zhuǎn)屏幕時(shí),該不會(huì)被系統(tǒng)殺死和重建,只會(huì)調(diào)用。因此可通過和來判斷是否被重建,并取出數(shù)據(jù)進(jìn)行恢復(fù)。但需要注意的是,在取出數(shù)據(jù)時(shí)一定要先判斷是否為空。只有在進(jìn)程不被掉,正常情況下才會(huì)執(zhí)行方法。 目錄介紹 1.0.0.1 說下Activity的生命周期?屏幕旋轉(zhuǎn)時(shí)生命周期?異常條件會(huì)調(diào)用什么方法? 1.0.0.2 后臺(tái)的Activity被系統(tǒng)回收怎么辦?說一下onSaveInsta...

    iamyoung001 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<