Android stores read and write files internally, and android reads and writes

Source: Internet
Author: User

Android stores read and write files internally, and android reads and writes

File read/write operations
* Ram memory: The Running memory, equivalent to the computer memory
* Rom memory: internal storage space, equivalent to the hard disk of a computer
* SD card: external storage space, equivalent to a mobile hard disk of a computer
Read and Write files in an internal Bucket
> Case study: Enter the account password, select "Remember account password", and click "Log on" to save the account and password at the same time. The interface is as follows:

1. Define the Layout

The Code is as follows:

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "android: orientation =" vertical "> <EditText android: id =" @ + id/et_name "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: hint = "enter account"/> <EditText android: id = "@ + id/pass" android: inputType = "textPassword" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: hint = "enter the password"/> <CheckBox android: id = "@ + id/cb" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Remember account and password"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: onClick = "click" android: text = "login"/> </LinearLayout>

The Code is as follows:

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "> <EditText android: id =" @ + id/et_name "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: hint = "Enter the Account"/> <EditText android: id = "@ + id/et_pass" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: hint = "enter the password" android: inputType = "textPassword"/> <RelativeLayout android: layout_width = "wrap_content" android: layout_height = "wrap_content"> <CheckBox android: id = "@ + id/cb" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerVertical = "true" android: text = "Remember account and password"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentRight = "true" android: onClick = "click" android: text = "login"/> </RelativeLayout> </LinearLayout>

Package com. wuyudong. rwinrom; import java. io. file; import java. io. fileOutputStream; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; import android. widget. checkBox; import android. widget. editText; import android. widget. toast; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void click (View v) {// obtain the user's input account and password EditText et_name = (EditText) findViewById (R. id. et_name); EditText et_pass = (EditText) findViewById (R. id. et_pass); String name = et_name.getText (). toString (); String pass = et_pass.getText (). toString (); // obtain the CheckBox component CheckBox cb = (CheckBox) findViewById (R. id. cb); // check whether the if (cb. isChecked () {saveAccount (name, pass) ;}// the Toast prompt box appears. makeText (this, "Logon successful", Toast. LENGTH_SHORT ). show ();} public void saveAccount (String name, String pass) {File file = new File ("data/com. wuyudong. rwinrom/info.txt "); try {FileOutputStream fos = new FileOutputStream (file); fos. write (name + "#" + pass ). getBytes (); fos. close ();} catch (Exception e) {// TODO: handle exception e. printStackTrace ();}}}

Before running the program, the folder in the internal storage photo is as follows:

The folder is empty.

Run the following interface:

Click "generate" to generate an info.txt file.

Export the file to your local computer

File Content: wuyudong #123456

4. Enable io stream to write files to internal storage

* Directly enable the file output stream to write data

// Persistently save data
File file = new File ("data/com. itheima. rwinrom/info.txt ");
FileOutputStream fos = new FileOutputStream (file );
Fos. write (name + "#" + pass). getBytes ());
Fos. close ();
* Check whether the file exists before reading data.

If (file. exists ())
* Reading the stored data is also directly reading the file input stream.

File file = new File ("data/com. itheima. rwinrom/info.txt ");
FileInputStream FCM = new FileInputStream (file );
// Change byte stream to byte stream
BufferedReader br = new BufferedReader (new InputStreamReader (FCM ));
String text = br. readLine ();
String [] s = text. split ("##");
* After the data is read, it is displayed in the input box.

Et_name.setText (s [0]);
Et_pass.setText (s [1]);
* An application can only create files under its own package name directory and cannot create files in other people's homes.

The final code is as follows:

Package com. wuyudong. rwinrom; import java. io. bufferedReader; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. inputStreamReader; import android. OS. bundle; import android. app. activity; import android. view. view; import android. widget. checkBox; import android. widget. editText; import android. widget. toast; public class MainActivity extends Activity {// File file = new File ("data/com. wuyudong. rwinrom/info.txt "); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); loadAccount ();} public void click (View v) {// obtain the account and password entered by the user EditText et_name = (EditText) findViewById (R. id. et_name); EditText et_pass = (EditText) findViewById (R. id. et_pass); String name = et_name.getText (). toString (); String pass = et_pass.getText (). toString (); // obtain the CheckBox component CheckBox cb = (CheckBox) findViewById (R. id. cb); // check whether the if (cb. isChecked () {saveAccount (name, pass) ;}// the Toast prompt box appears. makeText (this, "Logon successful", Toast. LENGTH_SHORT ). show ();} public void saveAccount (String name, String pass) {File file = new File ("data/com. wuyudong. rwinrom/info.txt "); try {FileOutputStream fos = new FileOutputStream (file); fos. write (name + "#" + pass ). getBytes (); fos. close ();} catch (Exception e) {e. printStackTrace () ;}} public void loadAccount () {File file = new File ("data/com. wuyudong. rwinrom/info.txt "); if (file. exists () {try {FileInputStream FCM = new FileInputStream (file); // convert byte stream to byte stream BufferedReader br = new BufferedReader (new InputStreamReader (FCM )); string text = br. readLine (); String [] s = text. split ("#"); // obtain the user-input account and password EditText et_name = (EditText) findViewById (R. id. et_name); EditText et_pass = (EditText) findViewById (R. id. et_pass); et_name.setText (s [0]); et_pass.setText (s [1]);} catch (Exception e) {e. printStackTrace ();}}}}

 

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.