Android-read and write data to the SD card

Source: Internet
Author: User

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 ();}}

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.