-android use the file class to read data files in the SD card

Source: Internet
Author: User

If you need to store data in your program using SDcard, you need to use the Androidmainfset.xml file
To configure the permissions:

Java code:
    1. <!--permissions to create and delete files in SD--
    2. <uses-permission
    3. Android:name="Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>


<!--Write permissions to the SD card--

Java code:
    1. <uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE"/>


Tips:
The knowledge points that will be used when reading and writing the SD card:
Static methods for the Environment class:
* Getdatadirectory (); Get the data directory in Android;
* Getexternalstorgedirectory (); Get to the external storage directory generally refers to the SD card;
* Getdownloadcachedirectory (); Gets the cache directory to download;
* Getexternalstoragestate (); Gets the current state of the external settings, SD card

Android for external settings state, we are more commonly used should be media_mounted (SD card exists and can be
Read and Write
* Getrootdirectory (); Get to the Android root path;
* isexternalstorageemulated (); Returns a Boolean value to determine whether the external setting is valid;
* Isexternalstorageremovable (); Determine if external settings can be removed;

The status of the external settings in Android is as follows:
* media_mounted SD card can read and write;
* Media_mounted_read_only SD card exists, can only be read operation
....................................................... [/b] [/color]
Case: effect implementation, click Implementation to write data to the SD card, the code is as follows:
Java code:

  1. /* Write the file data to SDcard, save the data */
  2. Ublic void Writedatatosd () {
  3. try{
  4. / * Get the file object and determine the information for the data file * /
  5. //file file = new file (environment.getexternalstoragedirectory () + "/f.txt");
  6. File File = new file (Environment.getexternalstoragedirectory (),"F.txt");
  7. /* Determine if the external settings of the SD can read/write */
  8. if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {
  9. /* The object of the stream */* * *
  10. FileOutputStream fos = new FileOutputStream (file);
  11. / * Data that needs to be written * /
  12. String message = "The weather is not very good";
  13. / * Convert string to byte array * /
  14. byte[] buffer = message.getbytes ();
  15. / * Start writing data * /
  16. Fos.write (buffer);
  17. / * Turn off the use of the stream * /
  18. Fos.close ();
  19. Toast.maketext (mainactivity.   This, "file write succeeded", " ()." Show ();
  20. }
  21. }catch (Exception ex) {
  22. Toast.maketext (mainactivity.   This, "file write Failed", + ). Show ();
  23. }

Read the files in the SD card:
/* Read Data */

Java code:
    1. Public void Readdatafromsd () {
    2. try{
    3. / * Create a file object to identify the information that needs to be read * /
    4. File File = new file (Environment.getexternalstoragedirectory (),"F.txt");
    5. / * Fileinputsteam The input stream object, * /
    6. FileInputStream FIS = new FileInputStream (file);
    7. / * Prepare a byte array for the user to load the data that will be read * /
    8. byte[] buffer = new byte[fis.available ()];
    9. / * Start to read the file * /
    10. Fis.read (buffer);
    11. / * Close stream * /
    12. Fis.close ();
    13. / * Convert a byte array into a character A and convert the encoded format * /
    14. String res = encodingutils.getstring (buffer, "UTF-8");
    15. Toast.maketext (mainactivity.   This, "The file read successfully, you read the data is:" +res, " ." Show ();
    16. }catch (Exception ex) {
    17. Toast.maketext (mainactivity. This, "file read failed!"  ", + ). Show ();
    18. }
    19. }

-android use the file class to read data files in the SD card

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.