Import java. io. bufferedReader; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. inputStreamReader; import android. content. context; import android. OS. environment; public class Fileservice {/*** Method for storing user passwords ** @ param context * @ param user * @ param psd */public static boolean saveUserInfo (Context context, string user, String psd) {/** SD card, that is, the external storage device of the mobile phone. When saving data in the SD card, the SD card must be given the permission for write operations, read operations do not require permissions */try {// TODO --- understand the constant if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {System. out. println ("External Storage Device path:" + Environment. getExternalStorageDirectory (); File file = new File (Environment. getExternalStorageDirectory (), "user.txt"); // File file = new File ("mnt/sdcard/", "user.txt "); this code is equivalent to the above Code. // The data to be stored String str = user + "#" + psd; // output stream, fileOutputStream out = new FileOutputStream (file); out. write (str. getBytes (); out. close (); return true;} else {return false;} catch (Exception e) {return false;} public static String getUserInfo (Context context) {try {// determine whether the SD card has been mounted. if it is mounted, It is readable and writable if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {File file = new File (Environment. getExternalStorageDirectory (), "user.txt"); // the following file stream operations are performed in the same way: FileInputStream fileInputStream = new FileInputStream (file); InputStreamReader inputStreamReader = new InputStreamReader (fileInputStream ); bufferedReader reader = new BufferedReader (inputStreamReader); // The stream operation object of BufferedReader. You can read a row of String str = reader. readLine (); return str;} else {return null ;}} catch (Exception e) {return null ;}}}
Pay attention to the read and write operations on SD card files. You need to add permissions. Android. permission. WRITE_EXTERNAL_STORAGE can be added using a visualization tool or manually added.