Android learning path-data usage (1)

Source: Internet
Author: User

Android learning path-data usage (1)

Today is the fourth day of Android learning. In the morning, I learned how to store and use data. Here, I will summarize the data in the morning.

Data storage is divided into four categories:

* File

* Sharedpreference (parameter)

* SQLite Database

* Content provider (Content provide)

Let's take a look at the first two methods: file and Sharedpreference.

1. File

The file here is exactly the same as the file in Java, but it is not described here. Check the process.

First, define two buttons on the main interface. One is "Write File" and the other is "Read File ".

Add listening events to write files

Writefile. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {String content = "Michael 20 male"; // FileOutputStream fos = new FileOutputStream ("test.txt "); fileOutputStream fos = null; try {fos = MainActivity. this. openFileOutput ("data.txt", Context. MODE_PRIVATE); fos. write (content. getBytes ();} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {if (fo S! = Null) {try {fos. close () ;}catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}}});
Add listening events to read files

readfile.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {FileInputStream in=null;try {in = MainActivity.this.openFileInput("data.txt");byte[] bytes = new byte[1024];int length=0;StringBuffer content = new StringBuffer();while((length=in.read(bytes))!=-1){content.append(new String(bytes,0,length));}Toast.makeText(MainActivity.this, content.toString(), Toast.LENGTH_SHORT).show();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(in!=null){try {in.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}});
When the program starts, click and write a file first. In this case, a data.txt file will be generated under the specific package. How can this file be seen!

We developed it using Eclipse. Let's look at it in this tool.

Vc7Sw8e1xLD8PC9wPgo8cD48aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20140918/2014091809041578.png" alt = "\">


For example, the data.txt file under files is the file we just wrote. in the upper-right circle, we can export the file to the computer, and then you can view the content in it. It will not be demonstrated here.

When you click read file, the Written string is displayed.

2. SharedPreferences

This is a package encapsulated in Android. The advantage is that it is convenient, and it is very convenient to access some data and write, as is the extraction.

Draw two buttons for reading and writing on the main interface.

Add listening events separately

Spwritefile. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// get the SharedPreferences object SharedPreferences sp = getSharedPreferences ("data", Context. MODE_PRIVATE); // get Editor ed = sp. edit (); // Add data ed. putString ("name", "Zhang San"); ed. putInt ("age", 20); // submit ed. commit () ;}}); spreadfile. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// get the SharedPreferences object SharedPreferences sp = getSharedPreferences ("data", Context. MODE_PRIVATE); String name = sp. getString ("name", "hot"); int age = sp. getInt ("age", 0); Toast. makeText (MainActivity. this, name + "---" + age, Toast. LENGTH_SHORT ). show ();}});
We can find that when writing data, we first get the SharedPreferences object, then get the editor, and then directly putString. Finally, don't forget to submit the commit statement. Otherwise, it is like the text file is not saved !!

Get is enough when you read the file. You can check the inverted file.

 
  
   
Zhang San
  
  
This file is automatically generated as an xml file, which contains Map, Key, and Value.

!!!!!!


Related Article

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.