Android [intermediate tutorial] Chapter 2 data storage File

Source: Internet
Author: User

Next, in the previous chapter, we stored the data in the File. The layout File remains unchanged. For details, see main. xml.

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

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

Android: orientation = "vertical" android: layout_width = "match_parent"

Android: layout_height = "match_parent">

<TextView android: layout_width = "wrap_content" android: id = "@ + id/textView1"

Android: textAppearance = "? Android: attr/textAppearanceLarge"

Android: layout_height = "wrap_content" android: text = "TextView"> </TextView>

<EditText android: id = "@ + id/editText1" android: layout_width = "match_parent"

Android: layout_height = "wrap_content" android: inputType = "number">

<RequestFocus> </requestFocus>

</EditText>

<LinearLayout android: layout_width = "match_parent"

Android: id = "@ + id/linearLayout2" android: layout_height = "wrap_content">

<Button android: layout_width = "wrap_content"

Android: layout_height = "wrap_content" android: id = "@ + id/addOne"

Android: text = "Wukong killed another monster"> </Button>

<Button android: layout_width = "wrap_content"

Android: layout_height = "wrap_content" android: id = "@ + id/read_edit"

Android: text = "reading data in Edit"> </Button>

</LinearLayout>

<LinearLayout android: layout_width = "match_parent"

Android: id = "@ + id/linearLayout1" android: layout_height = "wrap_content">

<Button android: layout_width = "wrap_content"

Android: layout_height = "wrap_content" android: id = "@ + id/save"

Android: text = "Data Storage"> </Button>

<Button android: layout_width = "wrap_content"

Android: layout_height = "wrap_content" android: id = "@ + id/read"

Android: text = "read data"> </Button>

<Button android: layout_width = "wrap_content"

Android: layout_height = "wrap_content" android: id = "@ + id/clear"

Android: text = "Clear Data"> </Button>

</LinearLayout>

 

</LinearLayout>

As in the previous chapter, it is also an EditText and several buttons.

 

Main java file:

 

 

Import java. io. FileInputStream;

Import java. io. FileNotFoundException;

Import java. io. FileOutputStream;

Import java. io. PrintStream;

 

Import android. app. Activity;

Import android. OS. Bundle;

Import android. view. View;

Import android. view. View. OnClickListener;

Import android. widget. Button;

Import android. widget. EditText;

Import android. widget. TextView;

 

Public class Shared_PreferencesDemo extends Activity implements OnClickListener

{

Private final String FILE_NAME = "demo. bin ";

Private int count = 0;

Private String str;

Private TextView text;

Private EditText edit_text;

Private Button add;

Private Button save;

Private Button read;

Private View clear;

Private Button read_edit;

@ Override

Protected void onCreate (Bundle savedInstanceState)

{

// TODO Auto-generated method stub

Super. onCreate (savedInstanceState );

SetContentView (R. layout. shared_preferences );

Str = "Wukong killed" + count + "only monsters .";

Text = (TextView) findViewById (R. id. textView1 );

Text. setText (str );

Edit_text = (EditText) findViewById (R. id. editText1 );

Edit_text.setText (String. valueOf (count ));

Add = (Button) findViewById (R. id. addOne );

Add. setOnClickListener (this );

Read_edit = (Button) findViewById (R. id. read_edit );

Read_edit.setOnClickListener (this );

Save = (Button) findViewById (R. id. save );

Save. setOnClickListener (this );

Read = (Button) findViewById (R. id. read );

Read. setOnClickListener (this );

Clear = (Button) findViewById (R. id. clear );

Clear. setOnClickListener (this );

}

// Button event monitoring

@ Override

Public void onClick (View v)

{

Switch (v. getId ()){

// Wukong killed another monster button event

Case R. id. addOne:

// Increase the number of monsters by 1

Count ++;

// Refresh the value in the TextView and EditText controls.

Refresh ();

Break;

// Read the data button event in Edit

Case R. id. read_edit:

// Retrieve the value in SharedPreferences

Count = Integer. parseInt (edit_text.getText (). toString ());

Refresh ();

Break;

Case R. id. save:

// Custom Data Writing Method

Write (String. valueOf (count ));

Break;

Case R. id. read:

// Assign a value to count for the read data

Count = Integer. parseInt (read (). trim ());

Refresh ();

Break;

Case R. id. clear:

Count = 0;

Write (String. valueOf (count ));

Refresh ();

Break;

}

}

/**

* Reading data from a file is a simple java application and I/O operation.

* @ Return

*/

Private String read ()

{

Try

{

// Open the file input stream

FileInputStream FCM = openFileInput (FILE_NAME );

Byte [] buffer = new byte [1024];

Int hasRead = 0;

StringBuffer sb = new StringBuffer ();

While (hasRead = FS. read (buffer)> 0 ){

Sb. append (new String (buffer, 0, hasRead ));

}

Return sb. toString ();

} Catch (Exception e)

{

E. printStackTrace ();

}

Return null;

}

/**

* Writing data to a file is also a simple IO operation.

* @ Param string

*/

Private void write (String string)

{

Try

{

// Open the file output stream in overwrite Mode

FileOutputStream fos = openFileOutput (FILE_NAME, 0 );

// Package FileOutputStream into PrintStream

PrintStream ps = new PrintStream (fos );

// Output file content

Ps. println (string );

Ps. close ();

} Catch (FileNotFoundException e)

{

E. printStackTrace ();

}

}

 

// Custom refresh

Private void refresh ()

{

Str = "Wukong killed" + count + "only monsters .";

Text. setText (str );

Edit_text.setText (String. valueOf (count ));

}

}

This is mainly for file I/O operations, which is also relatively simple. People who have learned JAVA should know the I/O operations. For details, refer to the code. I will not talk about it here ,:

 

 

The difference here is that the storage path is different. It also exists in the data/package File, but it is created under the File folder, as shown in the figure:

 

OK, this chapter is over. In fact, the Android storage method is relatively simple. You can use these two storage methods for some simple data, if the data is large or complex, you should use the SQLite database for storage. In the next chapter, we will introduce the operations of the SQLite database. Thank you.

From: kangkangz4 Column

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.