Android data storage,
When the app is running, you need to save some user information. For example, the logon status and account information. Android provides multiple methods to store user data.
1. key-value storage, implemented through SharedPreferences
SharedPreferences sp = getSharedPreferences ("test", Context. MODE_PRIVATE );
Sp. edit (). putString ("str", "hello"). commit ();
Log. e (TAG, "save1:" + sp. getString ("str", null ));
2. Save the file and write the data into the internal memory.
File file = getFilesDir ();
Log. e (TAG, "save2:" + file. getAbsolutePath ());
File f2 = new File (file, "test. log ");
Try {
F2.createNewFile ();
// Write data into the file
Log. e (TAG, "save2:" + f2.exists ());
} Catch (IOException e ){
E. printStackTrace ();
}
3. Save the database and read and write structured data through the SQLite Database
Create a database through openOrCreateDatabase or inherit from SQLiteOpenHelper
4. Save the network and save the data on the server through the http protocol
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 ();
}
Instance download: https://files.cnblogs.com/files/bruce2020/TestSQLite.zip