一、保存文件到手機(jī)內(nèi)存
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/** * 保存數(shù)據(jù)到手機(jī)rom的文件里面. * @param context 應(yīng)用程序的上下文 提供環(huán)境 * @param name 用戶名 * @param password 密碼 * @throws exception */ public static void savetorom(context context, string name , string password) throws exception{ //file file = new file("/data/data/com.itheima.login/files/info.txt"); file file = new file(context.getfilesdir(), "info.txt" ); //該文件在data下的files文件夾下getcachedir()在cache文件夾下 文件大小不要超過1mb fileoutputstream fos = new fileoutputstream(file); string txt = name+ ":" +password; fos.write(txt.getbytes()); fos.flush(); fos.close(); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
/** * 獲取保存的數(shù)據(jù) * @param context * @return */ public static map<string,string> getuserinfo(context context) { file file = new file(context.getfilesdir(), "info.txt" ); try { fileinputstream fis = new fileinputstream(file); //也可直接讀取文件string result = streamtools.readfromstream(fis); bufferedreader br = new bufferedreader( new inputstreamreader(fis)); string str = br.readline(); string[] infos = str.split( ":" ); map<string,string> map = new hashmap<string, string>(); map.put( "username" , infos[ 0 ]); map.put( "password" , infos[ 1 ]); return map; } catch (exception e) { e.printstacktrace(); return null ; } } //最后可以直接調(diào)用上面的方法讀取信息 map<string, string> map = getuserinfo( this ); if (map!= null ){ textview.settext(map.get(“username”)); } |
二、保存文件到sd卡
獲取手機(jī)sd空間的大小:
1
2
3
4
5
6
7
8
9
10
11
|
file path = environment.getexternalstoragedirectory(); statfs stat = new statfs(path.getpath()); long blocksize = stat.getblocksize(); long totalblocks = stat.getblockcount(); long availableblocks = stat.getavailableblocks(); long totalsize = blocksize*totalblocks; long availsize = blocksize * availableblocks; string totalstr = formatter.formatfilesize( this ,totalsize); string availstr = formatter.formatfilesize( this , availsize); tv.settext( "總空間" +totalstr+ "\n" + "可用空間" +availstr); |
加入寫外部存儲的權(quán)限:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<uses-permission android:name= "android.permission.write_external_storage" /> public static void save(string name ,string password) throws exception{ if (environment.getexternalstoragestate().equals(environment.media_mounted)){ file file = new file(environment.getexternalstoragedirectory(), "info.txt" ); //也可直接寫/sdcard/info.txt 先判斷sd卡是否存在 fileoutputstream fos = new fileoutputstream(file); string txt = name+ ":" +password; fos.write(txt.getbytes()); fos.flush(); fos.close(); // 使用randomaccessfile像文件追加內(nèi)容fileoutputstream會把原有的文件內(nèi)容清空 //randomaccessfile raf = new randomaccessfile(file,"rw"); //raf.seek(file.length()); 將文件指針移動到最后 //raf.write(name.getbytes()+password.getbytes()); //raf.close(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
//讀取文件 加入讀取權(quán)限 public static string read(){ try { if (environment.getexternalstoragestate().equals(environment.media_mounted)){ file sdcarddir = environment.getexternalstoragedirectory(); fileinputstream fis = new fileinputstream(sdcarddir.getcanonicalpath() + "info.txt" ); bufferedreader br = new bufferedreader( new inputstreamreader(fis)); stringbuilder sb = new stringbuilder( "" ); string line = null ; while ((line = br.readline())!= null ){ sb.append(line); } return sb.tostring(); } } catch (exception e) { e.printstacktrace(); } return null ; } |
三、sharedpreferences的使用
sharedpreference是開發(fā)中常用的一種存儲方式,主要存儲一些系統(tǒng)不變的參數(shù)如是否是第一次進(jìn)入應(yīng)用程序等,通過鍵值對的方式進(jìn)行存儲
可以存儲的類型:booleans, floats, ints, longs,strings.
getsharedpreferences() - 存儲多個參數(shù)
getpreferences() - 僅存儲一個參數(shù)并且不需要指定名字(key)
寫入的步驟:
sharedpreferences調(diào)用edit()得到一個editor對象
使用 putboolean() and putstring()添加值
提交事務(wù)完成存儲
讀取時:只需要調(diào)用sharedpreferences的getboolean() and getstring()
下面是示例代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
public class mysharedpreference { private context context; private sharedpreferences sp ; private editor edit; public mysharedpreference(context context){ this .context = context; } public boolean savemessage(string name,string pwd){ boolean flag = false ; sp = context.getsharedpreferences( "userinfo" ,context.mode_private); //mode定義了訪問的權(quán)限現(xiàn)在是本應(yīng)用可以訪問 edit = sp.edit(); edit.putstring( "name" , name); edit.putstring( "pwd" , pwd); flag = edit.commit(); //提交事務(wù)將數(shù)據(jù)持久化到存儲器中 return flag; } public map<string,object> getmessage(){ map<string,object> map = new hashmap<string, object>(); sp = context.getsharedpreferences( "userinfo" , context.mode_private); string name = sp.getstring( "name" , "" ); string pwd = sp.getstring( "pwd" , "" ); map.put( "name" , name); map.put( "pwd" ,pwd); return map; } } |