Android-read and write data to the SD card
// Write data to the SD card private void writeSDcard (String str) {try {// determine whether the SD card if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {// get the directory File sdDire = Environment of the SD card. getExternalStorageDirectory (); FileOutputStream outFileStream = new FileOutputStream (sdDire. getCanonicalPath () + "/test.txt"); outFileStream. write (str. getBytes (); outFileStream. close (); Toast. makeText (this, "Data Protection The text.txt file is saved. ", Toast. LENGTH_LONG ). show () ;}} catch (Exception e) {e. printStackTrace () ;}}// read data from the SD card private void readSDcard () {StringBuffer strsBuffer = new StringBuffer (); try {// determine whether SD if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {File file = new File (Environment. getExternalStorageDirectory (). getCanonicalPath () + "/test.txt"); // determine whether the file exists if (file. Exists () {// open the file input stream FileInputStream fileR = new FileInputStream (file); BufferedReader reads = new BufferedReader (new InputStreamReader (fileR); String st = null; while (st = reads. readLine ())! = Null) {strsBuffer. append (st);} fileR. close ();} else {Toast. makeText (this, "the file in this directory does not exist", Toast. LENGTH_LONG ). show () ;}} catch (Exception e) {e. printStackTrace ();} Toast. makeText (this, "The data read is:" + strsBuffer. toString () + "", Toast. LENGTH_LONG ). show ();}}