android資料存放區,
app在運行過程中,需要儲存使用者的一些資訊。例如登入狀態、賬戶資訊等,安卓提供了多種方式來儲存使用者的資料。
1.key-value 儲存,通過SharedPreferences實現
SharedPreferences sp = getSharedPreferences("test", Context.MODE_PRIVATE);
sp.edit().putString("str", "hello").commit();
Log.e(TAG, "save1: "+sp.getString("str",null) );
2.檔案儲存,把資料寫入內部儲存空間中
File file = getFilesDir();
Log.e(TAG, "save2: "+file.getAbsolutePath());
File f2 = new File(file,"test.log");
try {
f2.createNewFile();
//在檔案中寫入資料
Log.e(TAG, "save2: "+f2.exists());
} catch (IOException e) {
e.printStackTrace();
}
3.資料庫儲存,通過SQLite 資料庫讀寫結構化資料
通過openOrCreateDatabase建立資料庫或者繼承SQLiteOpenHelper實現
4.網路儲存,通過http協議把資料儲存在伺服器上
URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
} finally {
urlConnection.disconnect();
}
執行個體下載:https://files.cnblogs.com/files/bruce2020/TestSQLite.zip