Android store data to local files

Source: Internet
Author: User
<span id="Label3"></p><p><p>XML file</p></p><pre class="brush:xml;toolbar:false"><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:padding= "5dp"  >    <EditText         android:id= "@+id/qqnum"         android: Layout_width= "fill_parent"         android:layout_height= "wrap_content"        android:hint= "please Enter qq"         Android:inputtype= "number"         android:textsize= "20dp"          />    <EditText          android:id= "@+id/pass"         android:layout_width= "fill_parent"          android:layout_height= "wrap_content"          android:hint= "please Enter the password"         android:inputtype= " Textpassword "        android:textsize=" 20DP "                 />    < Checkbox         android:id= "@+id/rem"          android:layout_width= "wrap_content"          android:layout_height= "wrap_content"         android:text= "remember password"                  />     <button          android:id= "@+id/login"          Android:layout_width= "wrap_content"         android:layout_height= "wrap _content "        android:text=" Login "/></linearlayout></pre><p><p> Utils <br> </p> </p><pre class="brush:java;toolbar:false">package com.example.android22filelogin;import java.io.bufferedreader;import  Java.io.fileinputstream;import java.io.filenotfoundexception;import java.io.fileoutputstream;import  java.io.InputStreamReader;import java.util.HashMap;import java.util.Map;import  Android.text.textutils;public class utils {public static boolean saveuserinfo ( String username,string pwd) {string data=username+ "# #" +pwd; String path= "/data/data/com.example.android22filelogin/data.txt"; try {fileoutputstream out=new  fileoutputstream (path); out.write (data.getbytes ()); out.flush (); out.close (); return true;}  catch  (exception e)  {e.printstacktrace ();} return false;} Public static map<string,string> getuserinfo () {string path= "/data/data/ Com.example.android22filelogin/data.txt "; Try {bufferedreader reader=new bufferedreader (new  inputstreamreader (new&nbsp FileInputStream (path)); String data=reader.readline (); If (! Textutils.isempty (data) {string [] datas=data.split ("# #"); map<string, string> userinfo=new hashmap<string, string> (); userinfo.put (" Number ",  datas[0]); userinfo.put (" pwd ",  datas[1]); return userinfo;}}  catch  (exception e)  {e.printstacktrace ();} return null;}}</pre><p><p> Activity </p> </p><pre class="brush:java;toolbar:false">public class mainactivity extends activity implements onclicklistener { private edittext qqnum,pwd;private checkbox rem;private button but;      @Override     protected void oncreate (bundle savedinstancestate)  {        super.oncreate (savedinstancestate);         setcontentview (r.layout.activity_main);         qqnum= (EditText) Findviewbyid (r.id.qqnum);         pwd= ( EditText) Findviewbyid (r.id.pass);         rem= (CheckBox) Findviewbyid ( R.id.rem)         but= (Button) Findviewbyid (r.id.login);         but.setonclicklistener (this);         //echo Data    &nbsP;    map<string, string> userinfo=utils.getuserinfo ();         if (userinfo!=null)         {         qqnum.settext (userinfo.get ("number"));         pwd.settext (userinfo.get ("pwd"));        }     } @Overridepublic  void onclick (view v)  {//remember the number and password String num=qqnum.gettext ( ). toString (); String password=pwd.gettext (). toString (); if (textutils.isempty (num) | | Textutils.isempty (PASSWORD)) {toast.maketext (this,  "user name or password cannot be empty",  toast.length_long). show (); return;} Determines whether to remember the password if (rem.ischecked ()) {boolean issuccess=utils.saveuserinfo (num, password); Toast.maketext (this, issuccess+ "",  toast.length_long). show ();} Login successful}}</pre><p><p><br></p></p><p><p>This article is from the "java white battlefield" blog, Be sure to keep this source http://8023java.blog.51cto.com/10117207/1664699</p></p><p><p>Android store data to local files</p></p></span>

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.