Read/write operations on the SD card-Save the User Login Password

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.