Package com. example. f01_sdcard01; import java. io. file; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import android. OS. environment; public class FileUtiles {// when writing data to the SD card, remember to add a <uses-permission // android: name = "android. permission. WRITE_EXTERNAL_STORAGE "/> // pass two parameters. One is the file name and the other is the written data public void fileSave (String fileName, byte [] data) {FileOutpu TStream fileOutputStream = null; String state = Environment. getExternalStorageState (); if (Environment. MEDIA_MOUNTED.equals (state) {// create the sdcard directory File = new file (Environment. getExternalStorageDirectory (). getAbsolutePath () + "/txt"); // if the directory does not exist, create a file if (! File. exists () {file. mkdirs ();} try {// write data to fileOutputStream = new FileOutputStream (new File (file, fileName) in the specified File; try {fileOutputStream. write (data, 0, data. length);} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {if (fileOutputStream! = Null) {try {fileOutputStream. close ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}}}} public String readSdcard (String fileName) {// read the data of the specified fileIf (Environment. MEDIA_MOUNTED.equals (Environment. getExternalStorageState () {File file = new File (Environment. getExternalStorageDirectory (). getAbsolutePath () + "/txt/"); // if the directory does not exist, create a file if (file. exists () {File file2 = new File (file, fileName); InputStream inputStream = null; ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream (); try {inputStream = new FileInputStream (file2 ); byte [] data = ne W byte [1024]; int len = 0; try {while (len = inputStream. read (data ))! =-1) {arrayOutputStream. write (data, 0, len);} return new String (arrayOutputStream. toByteArray ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {if (inputStream! = Null) {try {inputStream. close ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}}} return null ;}