The use of data on the Android Learning Path (i)

Source: Internet
Author: User

Today is the fourth day of Android learning, the morning simple study of the storage and use of data, here to summarize the morning

Data storage is divided into four categories:

* File

* Sharedpreference (parameter)

* SQLite Database

* Content provider (contents provide)

Take a look at the first two methods, files and Sharedpreference

1. Documents

The files here are exactly the same as the files in Java, not the introduction, the process

First, define two buttons on the main interface, one is "write file" one is "read file", write first read

Adding listener events to a write file

Writefile.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {String content= "320 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 (fos!=null) {try {fos.close ()} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} }});
Add a listener event to the read file

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 ();}}}} );
Program run up, first click to write a file, this time the project will be more than a package of data.txt file, then not only to ask, how can this file to see it!

We developed it with Eclipse, and that's what it looks like in this tool.


The FileExplorer of the virtual machine will be found in the direction of the arrow, even if you use the phone test to find

OK, in the file directory open the data file, there is a data file, continue to open, and then inside the device all the app's package, find our Package



For example, files under the data.txt is the file we just wrote, in the upper right corner of the circle can be said to export files to the computer, and then you can view the contents, here no longer show

When you click the Read file, the written string is displayed

2, Sharedpreferences

This is a package in the Android package, the advantage is that convenient, access to some data writing is very convenient, extraction is the same

Also draw two buttons to read and write in the main interface

Add Listener Events separately

Spwritefile.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {// Get Sharedpreferences object Sharedpreferences sp = getsharedpreferences ("Data", context.mode_private);//Get Editor 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) {// Gets the Sharedpreferences object sharedpreferences sp = getsharedpreferences ("Data", context.mode_private); String name = sp.getstring ("name", "hot"); int = Sp.getint ("Age", 0); Toast.maketext (mainactivity.this, name+ "---" +age, Toast.length_short). Show ();});
We can find that when writing the data, get the Sharedpreferences object first, then get the editor, then directly putstring on the line, and finally do not forget commit commit, otherwise it is like the text file is not saved!!

Read the file, get on the line, this can look at the files poured out

<?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '? ><map><string name= "Name" > Zhang San </string ><int name= "Age" value= "/></map>"
This file is automatically generated as an XML file, which is Map,key and value

!!!!!!


The use of data on the Android Learning Path (i)

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.