新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > Android學(xué)習(xí)筆記-保存數(shù)據(jù)的實(shí)現(xiàn)方法1

        Android學(xué)習(xí)筆記-保存數(shù)據(jù)的實(shí)現(xiàn)方法1

        作者: 時(shí)間:2016-09-12 來源:網(wǎng)絡(luò) 收藏

        Android開發(fā)中,有時(shí)候我們需要對信息進(jìn)行保存,那么今天就來介紹一下,保存文件到內(nèi)存,以及SD卡的一些操作,及方法,供參考。

        本文引用地址:http://www.104case.com/article/201609/304699.htm

        第一種,保存數(shù)據(jù)到內(nèi)存中:

        //java開發(fā)中的保存數(shù)據(jù)的方式

        public static boolean saveUserInfo(String username,String password){

        File file = new File(/data/data/com.ftf.login/info.txt);

        try {

        FileOutputStream fos = new FileOutputStream(file);

        // ftf##123

        fos.write((username+##+password).getBytes());

        fos.close();

        } catch (Exception e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

        return false;

        }

        return true;

        }

        //Android開發(fā)中,保存數(shù)據(jù)的方法,我們傳遞一個(gè)context對象,這樣就可以較為直接的把數(shù)據(jù)保存到程序在手機(jī)系統(tǒng)中的單獨(dú)的文件夾,符合Android的開發(fā)規(guī)范,

        public static boolean saveUserInfo(Context context,String username,String password){

        try {

        File filesDir = context.getFilesDir();

        File file = new File(filesDir,info.txt);

        FileOutputStream fos = new FileOutputStream(file);

        // ftf##123

        fos.write((username+##+password).getBytes());

        fos.close();

        } catch (Exception e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

        return false;

        }

        return true;

        }

        /*

        * 獲取保存的數(shù)據(jù)

        */

        public static Map

        File filesDir = context.getFilesDir();

        File file = new File(filesDir,info.txt);

        try {

        FileInputStream fis = new FileInputStream(file);

        //使用buffer,

        BufferedReader br = new BufferedReader(new InputStreamReader(fis));

        String str = br.readLine();

        String[] infos = str.split(##);

        Map

        map.put(username, infos[0]);

        map.put(password, infos[1]);

        return map;

        } catch (Exception e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

        return null;

        }

        }

        第二種,保存數(shù)據(jù)到SD卡

        這時(shí)我們需要用到Environment,來較為方便的獲取SD卡的目錄,這時(shí)隨便一般情況下,SD卡是在/data/data/sdcard目錄下,但是一些國產(chǎn)手機(jī),以及平板中目錄機(jī)構(gòu)不是這樣的,這樣做可以保證程序的兼容性,而且也是Android開發(fā)規(guī)范推薦。

        public static boolean saveUserInfo(Context context,String username,String password){

        try {

        // File filesDir = context.getFilesDir();

        // File file = new File(filesDir,info.txt);

        if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()));

        //獲取SD卡的目錄

        File file = new File(Environment.getExternalStorageDirectory(),info.txt);

        FileOutputStream fos = new FileOutputStream(file);

        // ftf##123

        fos.write((username+##+password).getBytes());

        fos.close();

        } catch (Exception e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

        return false;

        }

        return true;

        }

        第三,按照權(quán)限,對文件進(jìn)行存儲(chǔ)

        這個(gè)較為符合Android的開發(fā)規(guī)范,Android下文件存儲(chǔ)有四中類型:PRIVATE,READEABLE,WRITEABLE,READEABLE+WRITEABLE,也即私有,可讀,可寫,可讀可寫,我們在保存文件的時(shí)候可以直接進(jìn)行指定,而且context可以直接打開一個(gè)文件輸出流,所以Android下開發(fā)保存文件,推薦這種方式。

        public static boolean saveUserInfo(Context context,String username,String password,int mode){

        try {

        //

        // File filesDir = context.getFilesDir();

        // File file = new File(filesDir,info.txt);

        // FileOutputStream fos = new FileOutputStream(file);

        //在上下文的環(huán)境創(chuàng)建一個(gè)文件

        FileOutputStream fos = null;

        switch (mode) {

        case 1:

        fos = context.openFileOutput(private.txt, Context.MODE_PRIVATE);

        break;

        case 2:

        fos = context.openFileOutput(readeable.txt, Context.MODE_WORLD_READABLE);

        break;

        case 3:

        fos = context.openFileOutput(writeable.txt, Context.MODE_WORLD_WRITEABLE);

        break;

        case 4:

        fos = context.openFileOutput(public.txt, Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);

        break;

        }

        // ftf##123

        fos.write((username+##+password).getBytes());

        fos.close();

        } catch (Exception e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

        return false;

        }

        return true;

        }



        關(guān)鍵詞:

        評(píng)論


        相關(guān)推薦

        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 华蓥市| 南乐县| 兴文县| 拉孜县| 仪征市| 余庆县| 化德县| 惠水县| 兴和县| 邯郸县| 黔西| 新晃| 香格里拉县| 巢湖市| 锡林郭勒盟| 梅河口市| 潞城市| 墨玉县| 正安县| 奉贤区| 行唐县| 会东县| 沭阳县| 永昌县| 淮滨县| 呼图壁县| 仪陇县| 慈溪市| 靖西县| 中西区| 石台县| 通江县| 寻甸| 兴和县| 余姚市| 磐石市| 兰溪市| 如皋市| 樟树市| 咸阳市| 海丰县|