Write data to sd card private void Writesdcard (String str) {try {//To determine if there is an SD card if (environment . Getexternalstoragestate (). Equals (environment.media_mounted)) {//Get the SD card directory File Sddire = Environment.getexternalstoragedirectory (); FileOutputStream Outfilestream = new FileOutputStream (Sddire.getcanonicalpath () + "/test.txt"); Outfilestream.write (Str.getbytes ()); Outfilestream.close (); Toast.maketext (This, "Data saved to Text.txt file", Toast.length_long). Show (); }} catch (Exception e) {e.printstacktrace (); }}//Read data from SD card private void Readsdcard () {StringBuffer strsbuffer = new StringBuffer (); try {//To determine if there is an SD if (Environment.getexternalstoragestate (). Equals (Enviro Nment. media_mounted)) { File File = new file (Environment.getexternalstoragedirectory (). Getcanonicalpath () + "/test.txt"); Determine if the file exists if (file.exists ()) {//Open 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, "file does not exist in this directory", Toast.length_long). Show (); }}} catch (Exception e) {e.printstacktrace (); } toast.maketext (This, "The data read is:" + strsbuffer.tostring () + "", Toast.length_long). Show (); } }
android--Read and write data to SD card