Data stored in the File format in Android [Android evolution thirty]

Source: Internet
Author: User

 

Previously, I briefly introduced two data storage methods, for example, Shared Preferences, in [Android evolution 14th], I wrote an example of the address book for storing data in the sqlite database. Next I will introduce the format of storing data in File. I will talk about two points in File format, files cannot be shared among different programs. Another feature is to use the openFileOutput method to open a file (create this file if the file does not exist). openFileOutput saves things to the file. I wrote an example: click the button and write a count of the int type in the file. divide it by 2 to get the remainder. If the remainder is 0, the status is off, if the remainder is 1, the status is on. The count value is always ++. This is a simple example. Reprinted please indicate the source: http://blog.csdn.net/wdaming1986/article/details/6849703

 

See: click the button and click the button again.


 

This is the content in the dmfile. cfg file: mButton always ++. If it is an odd number, mChange = false. If it is an even number, it is true.

 

 

Code: In this FileStorage project:

In the com.cn. daming package: Content in the FileStorageMainActivity. java class:

 

<Span style = "font-size: 13px; color: #000000;"> package com.cn. daming;

 

Import java. io. FileInputStream;

Import java. io. FileNotFoundException;

Import java. io. FileOutputStream;

Import java. io. IOException;

Import java. util. Properties;

 

Import android. app. Activity;

Import android. content. Context;

Import android. OS. Bundle;

Import android. view. KeyEvent;

Import android. view. View;

Import android. view. View. OnClickListener;

Import android. widget. Button;

Import android. widget. TextView;

Import android. widget. Toast;

 

Public class FileStorageMainActivity extends Activity {

Private TextView mTextView;

Private Button cButton;

Private boolean mChanged = false;

Public int mCount = 0;

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

MTextView = (TextView) findViewById (R. id. show_textview );

CButton = (Button) findViewById (R. id. button );

// Read the mChanged is true or false

ReadLoadFile ();

RefreTextView ();

InitButton ();

}

 

Private void initButton ()

{

CButton. setOnClickListener (new OnClickListener (){

Public void onClick (View arg0 ){

ReadLoadFile ();

If (mCount % 2 = 0)

{

MChanged = false;

RefreTextView ();

}

If (mCount % 2 = 1)

{

MChanged = true;

RefreTextView ();

}

MCount ++;

SaveFileStorage ();

}

});

}

 

Public void refreTextView ()

{

If (mChanged)

{

MTextView. setText ("the current status is: open! ");

}

Else

{

MTextView. setText ("the current status is off! ");

}

}

@ Override

Public boolean onKeyDown (int keyCode, KeyEvent event ){

If (keyCode = KeyEvent. KEYCODE_BACK)

{

// Save the fileStorage before the programe back

SaveFileStorage ();

If (mChanged)

{

Toast. makeText (this, "the exit state of the program is open! ", Toast. LENGTH_LONG). show ();

}

Else

{

Toast. makeText (this, "the exit status of the program is off! ", Toast. LENGTH_LONG). show ();

}

This. finish ();

Return true;

}

Return super. onKeyDown (keyCode, event );

}

Private void readLoadFile ()

{

// Make the Properties

Properties properties = new Properties ();

Try {

FileInputStream istream = this. openFileInput ("dmfile. cfg ");

Properties. load (istream );

}

Catch (FileNotFoundException e ){

Return;

}

Catch (IOException e ){

Return;

}

MChanged = Boolean. valueOf (properties. getProperty ("mChanging"). toString ());

MCount = Integer. valueOf (properties. getProperty ("mButton"). toString ());

}

Private boolean saveFileStorage ()

{

Properties properties = new Properties ();

Properties. put ("mChanging", String. valueOf (mChanged ));

Properties. put ("mButton", String. valueOf (mCount ));

Try {

FileOutputStream ostream = this. openFileOutput ("dmfile. cfg", Context. MODE_WORLD_WRITEABLE );

Properties. store (ostream ,"");

}

Catch (FileNotFoundException e)

{

Return false;

}

Catch (IOException e)

{

Return false;

}

Return true;

}

} </Span>

 

Content in the main. xml file:

 

 

<? Xml version = "1.0" encoding = "UTF-8"?>

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Android: orientation = "vertical"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

>

<TextView

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "@ string/hello"

Android: gravity = "center_vertical | center_horizontal"

Android: layout_marginBottom = "15dip"

Android: layout_marginTop = "15dip"

/>

<Button

Android: id = "@ + id/button"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "test changing File content"

Android: layout_marginBottom = "10dip"

/>

<TextView

Android: id = "@ + id/show_textview"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: gravity = "center"

/>

</LinearLayout>

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.