From: http://blog.csdn.net/jianghuiquan/article/details/8569233
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <ButtonAndroid:id= "@+id/save"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Save Data (File)" /> <ButtonAndroid:id= "@+id/read"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "read data (File)" /></LinearLayout>
PackageCom.example.yanlei.wifi;ImportAndroid.os.Bundle;Importandroid.os.Environment;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.Toast;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.PrintStream;ImportJava.util.Scanner; Public classMainactivityextendsappcompatactivity {PrivateButton btnsave=NULL; PrivateButton btnread=NULL; PrivateFile file=NULL; Private Static FinalString filename= "Data.txt"; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Btnsave= (Button)Super. Findviewbyid (R.id.save); Btnread= (Button)Super. Findviewbyid (R.id.read); Btnsave.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {printstream ps=NULL; //determine if an external memory card exists if(!environment.getexternalstoragestate (). Equals (environment.media_mounted)) {Toast.maketext (Getapplicationcontext (),"Read failed, SD memory card does not exist!" ", Toast.length_long). Show (); return; } //Initialize fileString path=environment.getexternalstoragedirectory (). toString ()+File.separator+ "Genwoxue" +File.separator+FILENAME; File=NewFile (path); //if the parent folder of the current file does not exist, create the Genwoxue folder if(!File.getparentfile (). exists ()) File.getparentfile (). Mkdirs (); //Write a file Try{PS=NewPrintStream (Newfileoutputstream (file)); Ps.println ("Follow me to learn website: www.genwoxue.com"); Ps.println (""); Ps.println ("Email: [Email protected]"); } Catch(FileNotFoundException e) {e.printstacktrace (); }finally{ps.close (); } toast.maketext (Getapplicationcontext (),"Saved successfully!" ", Toast.length_long). Show (); } }); Btnread.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {StringBuffer info=NewStringBuffer (); //determine if an external memory card exists if(!environment.getexternalstoragestate (). Equals (environment.media_mounted)) {Toast.maketext (Getapplicationcontext (),"Read failed, SD memory card does not exist!" ", Toast.length_long). Show (); return; } //Initialize fileString path=environment.getexternalstoragedirectory (). toString ()+File.separator+ "Genwoxue" +File.separator+FILENAME; File=NewFile (path); if(!file.exists ()) {Toast.maketext (Getapplicationcontext (),"File does not exist!" ", Toast.length_long). Show (); return; } //Read File contentsScanner scan=NULL; Try{Scan=NewScanner (Newfileinputstream (file)); while(Scan.hasnext ()) {Info.append (Scan.next ()). Append ("☆☆☆\n"); } toast.maketext (Getapplicationcontext (), info.tostring (), Toast.length_long). Show (); } Catch(FileNotFoundException e) {e.printstacktrace (); }finally{scan.close (); } } }); }}
Permissions
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.yanlei.wifi" > <!--Create and delete file permissions in SDcard - <uses-permissionAndroid:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!--Write Data permission to SDcard - <uses-permissionAndroid:name= "Android.permission.READ_EXTERNAL_STORAGE"/> <uses-permissionAndroid:name= "Android.permission.WRITE_EXTERNAL_STORAGE" /> <ApplicationAndroid:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name" > <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> </Application></Manifest>
<uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission Android : Name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
Android data file access to memory card