The so-called external storage external storage is to save the data file on the SD card.
1. However, the status of the SD card needs to be judged before the data is saved.
Use Environment.getexternalstoragestate () to run status of SD card
There's a state of one, respectively.
MEDIA_UNKNOWN, MEDIA_REMOVED, MEDIA_UNMOUNTED, MEDIA_CHECKING, MEDIA_NOFS, MEDIA_MOUNTED, MEDIA_MOUNTED_READ_ONLY, MEDIA_SHARED,MEDIA_BAD_REMOVAL, orMEDIA_UNMOUNTABLE.
We generally use the
media_mounted operable
2. Get the SD card with the directory
Here you can use the Environment.getexternalstoragedirectory () method to get
3. Determine if the path exists and operate accordingly
Use File.getparentfile (). exists () to determine if the file path exists, and if it does not exist, create
File.getparentfile (). mkdir ();
4. Write data using methods such as Outputstream,writer,printstream
5. Read the file in the same way
XML file
<span style= "FONT-SIZE:18PX;" ><relativelayout 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:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <button android:id=" @+id/button1 "android:layout_width=" Wrap_content "Androi d:layout_height= "Wrap_content" android:layout_alignparenttop= "true" android:layout_centerhorizontal= "true" android:layout_margintop= "106DP" android:text= "Save sd Card File"/> <button android:id= "@+id/button2" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_centerhorizon Tal= "true" Android:layout_centervertical= "true" android:text= "read SD card file"/></relativelayout></span>
java files
<span style= "FONT-SIZE:18PX;" >package Com.example.externalstorage;import Java.io.file;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.outputstream;import Java.io.printstream;import Java.util.Scanner;import Android.os.bundle;import Android.os.environment;import Android.app.activity;import Android.view.Menu;import Android.view.view;import Android.widget.button;import Android.widget.toast;public class MainActivity extends Activity {private Button saveData, getdata;public static final String filename = "Flyou.dat";//File name public static final STR ing filedir = "myFile";//file path under SD card private PrintStream print;//Initialize print stream private Scanner input;//Initialize input stream @overrideprotected V OID onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main );//Instantiate button SaveData = (button) This.findviewbyid (r.id.button1); getData = (Button) This.findviewbyid (r.id.button2);// Save files to SD card Savedata.setonclicklistener (new View.onclicklistener (){@Overridepublic void OnClick (View v) {//TODO auto-generated method stub//Gets the status of the SD card and determines whether string state is available = Environment.gete Xternalstoragestate (); if (Environment.MEDIA_MOUNTED.equals (state)) {//Get SD card with directory string sdpath = Environment.getexternalstoragedirectory (). toString ();//Patchwork full file address string path = Sdpath + File.separator + filedir+ File.separator + filename;//System.out.println (path);//Get the file Object File File = new file (path);//Determine if the path exists in the non-existent then automatically create if (! File.getparentfile (). exists ()) {File.getparentfile (). mkdir (); try {//use a print stream redirection write location to write data to the file print = new PrintStream (file);p Rint.print ("This is a data file accessed using the print stream on Sdka");p rint.println ("\ r \ n The second row of data ");//In the file default" \ r \ n "for newline characters} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Finally {//closes the print stream Toast.maketext (mainactivity.this, "File saved successfully, file location:" +path, 2). Show ();p rint.close ();}}); /Read file Getdata.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO Auto-generated method Stubstring state = EnviroNment.getexternalstoragestate (); if (Environment.MEDIA_MOUNTED.equals (state)) {String Sdpath = Environment.getexternalstoragedirectory (). toString (); String Path = Sdpath + File.separator + filedir+ file.separator + FILENAME; File File = new file (path); try {//Create Scanner rewrite output address = Scanner = new///To determine if there is a row of data while (Input.hasnext ()) {Strin G info = (String) input.next ();//The Data Toast.maketext (Mainactivity.this, info, 2) is read according to the toast display. Show ();}} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();} finally {Input.close ()}}});}} </span>
Finally, you must also configure permissions for Android engineering
Android.permission.WRITE_EXTERNAL_STORAGE
Open a file after exporting it with Notepad
Note:
If you are only reading from a file, you need to configure Android.permission.READ_EXTERNAL_STORAGE
If you are reading at the same time, you only need to configure Android.permission.WRITE_EXTERNAL_STORAGE.
Next forecast: SQLite database storage
Learn android< data storage from scratch (3) External storage external storage. 37 .>