新聞中心

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

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

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

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

本文引用地址:http://butianyuan.cn/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ù)的方法,我們傳遞一個context對象,這樣就可以較為直接的把數(shù)據(jù)保存到程序在手機系統(tǒng)中的單獨的文件夾,符合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卡

這時我們需要用到Environment,來較為方便的獲取SD卡的目錄,這時隨便一般情況下,SD卡是在/data/data/sdcard目錄下,但是一些國產(chǎn)手機,以及平板中目錄機構不是這樣的,這樣做可以保證程序的兼容性,而且也是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;

}

第三,按照權限,對文件進行存儲

這個較為符合Android的開發(fā)規(guī)范,Android下文件存儲有四中類型:PRIVATE,READEABLE,WRITEABLE,READEABLE+WRITEABLE,也即私有,可讀,可寫,可讀可寫,我們在保存文件的時候可以直接進行指定,而且context可以直接打開一個文件輸出流,所以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)建一個文件

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;

}



關鍵詞:

評論


相關推薦

技術專區(qū)

關閉