Android: Daily Learning Notes (9) ——— Explore Persistent technologies

Source: Internet
Author: User
Tags sqlite database

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:

  1. Called using the file name and operation mode openFileOutput() . This will return one FileOutputStream .
  2. Use write() write to file.
  3. Use close() off-stream transmission.

To read a file from an internal store:

  1. Invokes openFileInput() and passes the name of the file to be read. This will return one FileInputStream .
  2. Use to read() Read file bytes.
  3. 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:

    1. Called edit() to get SharedPreferences.Editor .
    2. putBoolean() putString() add values by using and so on.
    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.