Android data storage I/O
In Android development, local data storage is inevitable. Today we will talk about how to store data using IO streams.
Here we simulate a demo of the QQ login interface to actually operate the IO stream.
Function Description: click the button to save the user name and password entered by the user. When you click Remember password, the user name and password can be displayed when the application is opened for the second time.
1. The layout file code will not be pasted here.
2. MainActivity. java
Package com. example. viewswitchtest; import java. io. bufferedReader; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. inputStreamReader; import java. util. arrayList; import java. util. list; import android. OS. bundle; import android. OS. environment; import android. app. activity; import android. graphics. drawable. drawable; import android. telephony. smsManager; import android. util. log; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. checkBox; import android. widget. editText; import android. widget. toast; public class MainActivity extends Activity {private EditText editAccount; private EditText editPwd; private Button btnLogin; private CheckBox checkbox; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // account edit editAccount = (EditText) findViewById (R. id. edit_account); // password edit editPwd = (EditText) findViewById (R. id. edit_pwd); // log on to the button btnLogin = (Button) findViewById (R. id. btn_login); // remember the account and password checkbox = (CheckBox) findViewById (R. id. checkbox); // display the account and password readAccountAndPwd (); btnLogin. setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {if (checkbox. isChecked () {String account = editAccount. getText (). toString (); String pwd = editPwd. getText (). toString (); // android provides two API // getFilesDir () --> paths to prevent errors in the developer's handwritten path: data/project package name/files // getCacheDir () --> path: data/project package name/cache File file = new File (getFilesDir (), info.txt); try {FileOutputStream fos = new FileOutputStream (file); fos. write (account +-+ pwd ). getBytes (); // Save the format as account-password to facilitate the separation of fos. close ();} catch (Exception e) {e. printStackTrace () ;}} Toast. makeText (MainActivity. this, login success, Toast. LENGTH_LONG ). show () ;}}) ;}/ *** echo user name and password */private void readAccountAndPwd () {File file = new File (getFilesDir (), info.txt ); if (file. exists () {try {FileInputStream FCM = new FileInputStream (file); BufferedReader reader = new BufferedReader (new InputStreamReader (FS); String text = reader. readLine (); String [] infos = text. split (-); // Echo information editAccount. setText (infos [0]); editPwd. setText (infos [1]); FCM. close (); reader. close ();} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace ();}}}}
3. Enter the account and password, and click the login button. We can see the saved txt format file in file e in DDMS:
4. Export the file and open it in the text editor to verify that the account and password are correct:
You can see that the account and password have been properly saved and the format is correct.
5. Close the application and open it again.
We can see that the data can be correctly displayed in the input box:-D !!!