android几种数据存储方式

[size=x-large][color=green]android数据存储方式[/color][/size]

[size=large][color=yellow]1:SharedPreferences存储数据。
2:ContentProvider存储
3:文件存储
4:SQLlite存储 
5:网络存储[/color]

按照个人理解,SharedPreferences存储数据原来上来说属于内部存储,所以可以理解为
[color=yellow]1:内部存储
2:ContentProvider存储
3:外部存储
4:SQLlite存储 
5:网络存储 8) [/color]

[color=blue]1:SharedPreferences存储数据[/color]。这个简单,而且因为是内存存储,所以特别快[/size]
SharedPreferences sp = getSharedPreferences("mysp", Context.MODE_PRIVATE);


[size=large][color=blue]内部存储:[/color]速度突出一个快[/size]
private void saveCurrentText(){

try {
OutputStream os = openFileOutput("data", Context.MODE_PRIVATE);
os.write(et.getText().toString().getBytes("utf-8"));
os.flush();
os.close();

Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
return;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();

}

private void readSavedText(){

try {
InputStream is = openFileInput("data");
byte[] bytes = new byte[is.available()];
is.read(bytes);
is.close();

String str = new String(bytes,"utf-8");
et.setText(str);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}


[size=large][color=blue]2:ContentProvider存储[/color]
本博客其他的文章已经介绍过了,这里就不讲了

[color=blue]3:文件存储[/color]
上面直接上个读写SD卡的例子吧(只能读写小文件,否则内存就超了)
[/size]
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

File dir = Environment.getExternalStorageDirectory();

File dataFile = new File(dir, "data.txt");
//read
try {
FileInputStream fis = new FileInputStream(dataFile);

byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fis.close();

String str = new String(bytes,"utf-8");
System.out.println(str);

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

//write
try {
if (!dataFile.exists()) {
dataFile.createNewFile();
}

FileOutputStream fos = new FileOutputStream(dataFile);
fos.write(new String("Hello eoe").getBytes("utf-8"));
fos.flush();
fos.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


[size=large][color=blue]4:SQLlite存储[/color] 
只要mysql或者oracle语句使用的多了,也没有太大问题,注意使用后关掉就行了,另外还特意写了篇文章介绍sqlite, :evil:

[color=blue]5:网络存储[/color],就是使用webservice解析的数据,或者从http协议中直接拿到的数据。[/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值