[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]
[size=large][color=blue]内部存储:[/color]速度突出一个快[/size]
[size=large][color=blue]2:ContentProvider存储[/color]
本博客其他的文章已经介绍过了,这里就不讲了
[color=blue]3:文件存储[/color]
上面直接上个读写SD卡的例子吧(只能读写小文件,否则内存就超了)
[/size]
[size=large][color=blue]4:SQLlite存储[/color]
只要mysql或者oracle语句使用的多了,也没有太大问题,注意使用后关掉就行了,另外还特意写了篇文章介绍sqlite, :evil:
[color=blue]5:网络存储[/color],就是使用webservice解析的数据,或者从http协议中直接拿到的数据。[/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]