Android: Daily Learning Notes (9) ——— Explore persistence technology into persistence technology what is persistence technology
Persistence technology means that the instantaneous data in memory is saved to the storage device, so that the data is not lost even when the phone or computer shuts down.
Three types of persistence technologies available from the Android system:
File storage, Sharedpreference (using shared preferences) storage, and database storage.
File storage
Description
You can save files directly in the internal storage of your device. By default, files that are saved to an internal store are private files for the app, and other apps (and users) cannot access the files. These files are also removed when the user uninstalls your app.
To create a private file and write to the internal store:
- Called using the file name and operation mode
openFileOutput()
. This will return one FileOutputStream
.
- Use
write()
write to file.
- Use
close()
off-stream transmission.
To read a file from an internal store:
- Invokes
openFileInput()
and passes the name of the file to be read. This will return one FileInputStream
.
- Use to
read()
Read file bytes.
- Then use the
close()
off-stream transfer.
General Code
Public voidsave1 (String text) {FileOutputStream out=NULL; BufferedWriter writer=NULL; Try{ out= Openfileoutput ("Data", mode_private); Writer=NewBufferedWriter (NewOutputStreamWriter (out)); Writer.write (text); } Catch(IOException e) {e.printstacktrace (); }finally { if(writer!=NULL) Try{writer.close (); } Catch(IOException e) {e.printstacktrace (); } } } Public voidRead1 () {FileInputStream in=NULL; BufferedReader Reader=NULL; StringBuilder content=NewStringBuilder (); Try{ in= Openfileinput ("Data"); Reader=NewBufferedReader (NewInputStreamReader (in)); String Line=NULL; while((Line=reader.readline ())! =NULL) Content.append (line); Toast.maketext (storeactivity. This, Content,toast.length_short). Show (); } Catch(IOException e) {e.printstacktrace (); }finally { Try{reader.close (); } Catch(IOException e) {e.printstacktrace (); } } }
Sharedpreference Storage
Description
Unlike how files are stored, shardpreference stores data in a way that uses key-value pairs. You can use it SharedPreferences
to save any raw data : Boolean, floating-point, Integer, Long, and string. This data will be persisted across multiple user sessions (even if your app has been terminated).
Get Sharedpreferences object:
getSharedPreferences()
-Use this method if you need more than one preference file identified by name (specified with the first parameter).
getPreferences()
-Use this method if you only need a preference file for Activity. Because this will be the only preference file for Activity, there is no need to provide a name. This means that the currently active class name is the filename.
Write Value:
- Called
edit()
to get SharedPreferences.Editor
.
putBoolean()
putString()
add values by using and so on.
- Using the
commit()
submit new value
Read-in Value:
To read the values, use getBoolean()
the and getString()
et SharedPreferences
methods.
General Code
public void Save2 () { sharedpreferences.editor Editor = Getsharedprefe Rences ("Data" ,mode_private). Edit (); Editor.putstring ( "name", "Tom" "age", 20 public void Read2 () { sharedpreferences sharedpreferences = getsharedpreferences (" Data "
,mode_private); LOG.D (
"storeactivity", Sharedpreferences.getstring ("name", "" " "storeactivity", "" "+sharedpreferences.getint (" Age ", 100
SQLite Database Storage
Android: Daily Learning Notes (9) ——— Explore Persistent technologies