Android saves files

Source: Internet
Author: User

Background

We use a common CheckBox to show whether to save the account and password when logging on.

The last step is to save the txt file and use Java I/O operations.

This is just a rough one.

Save files

Public static void savefile2card (Context context, String username, String password) {File file = null; FileOutputStream fos = null; try {// file = new File ("/data/com. yuyidong. savefile/savefile.txt "); file = new File (context. getFilesDir (), "info.txt"); fos = new FileOutputStream (file); fos. write (username + "!!!! "+ Password ). getBytes ();} catch (Exception e) {// catch Block e automatically generated by TODO. printStackTrace (); try {fos. close ();} catch (IOException e1) {// catch Block e1.printStackTrace () generated automatically by TODO ();}}}

Read files

Public static Map <String, String> getSaveFile (Context context) {File file = new File (context. getFilesDir (), "info.txt"); try {FileInputStream FCM = new FileInputStream (file); BufferedReader br = new BufferedReader (new InputStreamReader (FCM); String str = br. readLine (); String [] infos = str. split ("!!!! "); Map <String, String> map = new HashMap <String, String> (); map. put ("username", infos [0]); map. put ("password", infos [1]); br. close (); return map;} catch (Exception e) {// catch Block e automatically generated by TODO. printStackTrace (); return null ;}finally {}}

Main Program

public class MainActivity extends Activity {    private Button button;    private CheckBox check;    private EditText usernameText;    private EditText passwordText;    private String username;    private String password;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (Button) findViewById(R.id.button);        check = (CheckBox) findViewById(R.id.check);
        usernameText = (EditText) findViewById(R.id.username);                passwordText = (EditText) findViewById(R.id.password);
            button.setOnClickListener(new buttonListener());
Map <String, String> map = read. getSaveFile (this); usernameText. setText (map. get ("username"); passwordText. setText (map. get ("password");} class buttonListener implements OnClickListener {@ Override public void onClick (View v) {// The method stub username = usernameText automatically generated by TODO. getText (). toString (); password = passwordText. getText (). toString (); System. out. println (username + "~!! ~ "+ Password); if (check. isChecked () {save. savefile2card (MainActivity. this, username, password );}}}

I am the dividing line of tiantiao

Source code: http://pan.baidu.com/s/1dD1Qx01

SaveFile.zip

 

 

 

Reprinted please indicate the source: http://www.cnblogs.com/yydcdut/p/3708964.html

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.