android-Data Persistence Storage

Source: Internet
Author: User
Tags sqlite database

1. Data Persistence Storage

Android offers 4 ways to store files: files (/data/data/< package name >/files/),sharepreference(/data/data/ < package name >/shared prefs/),
SQLite Database (/data/data/< package name >/database/) and Content Provider.

Android uses the "java.io.*" library to provide an input-output (I/O) interface, where all files are read/written in stream form.

2. File storage

Under the context class, there is openfileoutput(string name,int mode),openfileinput(string name)

method to write or read a file. Name indicates the name of the file, not including the path, is already the default path for the application under the package

data/data/< the package name >/files/directory, when the file does not exist, it is created and placed under the default path, and a FileNotFoundException exception is thrown.

The value of mode is the constant value of the context class, as follows:

Mode_private: Indicates that the file can only be accessed by this app

MODE Append_:  表示新的内容会添加到原文件内容的后面

 MODE_WORLD_READABLE:表示该文件能被所有应用读取

 MODE_WORLD_WRITEABLE:表示该文件能被所有应用写入

2.1读取资源文件

     android上的应用可以读取资源文件的内容。资源文件必须存储在项目的res/raw/目录下。

      Activity可以通过getResources()方法获取Resources类对象。

     Resources类提供了openRawResource(int id)方法读取res/raw/目录下的文件,id是指该文件在R.java中生成对应的id值。

      将通过InputStream读取流读取的数据放入ByteArrayOutputStream流内。The data for the byte array output stream is written to a byte array.

The buffer grows automatically as data is written continuously. Data can be used toByteArray() and toString() obtained. Closing bytearrayoutputstream is not valid.

The methods in this class can still be called after the stream is closed, without generating any ioexception.即可以能够读取任意长度的文件里的内容。

3.SharedPreferences

  Preference是一种轻量级的键值存储方式,可以用它来持久保存一些变量的值,这些变量必须是基本数据类型的,

  它存储的数据保存在一个xml文件里,该xml文件存储在/data/data/<包名>/shared prefs/目录下可以通过,

  SharedPreferences对preference存储的数据操作,它可以通过Context类的getSharedPreferences(String name,int type)

  方法获取唯一一个SharedPreferences对象。同一个应用只有唯一的SharedPreferences对象,其记录文件(xml文件)可以被

  同一应用下的不同Activity共享,type有默认值:MODE_PRIVATE。

 注意:getSharedPreferences(String name,int type)后将会在/data/data/<包名>/shared prefs/下以type模式自动创建一个名为“name”的xml文件。

  写操作:

   SharedPreferencesEditor edit();//由SharedPreferences对象的该方法获取Editor,它负责写操作。

  Editor常用方法:

       putString (String name,String value);//

       clear();//清除键值

Remove (String key)

Commit ();//The method must finally have, it performs data change validation work.

Read operation:

The GetString (String name,string value) of the Sharedpreferences object, or//value the value is returned by default if there is no get to value.

android-Data Persistence Storage

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.