Android Data Storage--file storage

Source: Internet
Author: User

The file store data uses IO operations in Java to save and read files. The main actions for files are the following three types:1. Create or open a file2. Reading files3. deleting filesTo create a file:1. Get the output stream via Context.openfileoutput     /*
* Name to open or create a file
* Mode to open or create a file
*/

FileOutputStream openfileoutput (String name, int mode)
Tip: The saved file created is stored in the/data/data/<package name>/files folder2. Four types of file saving modes context.mode_private = 0
Context.mode_world_readable = 1
Context.mode_world_writeable = 2
Context.mode_append = 32768
Mode_private is the default mode of operation, which means that the file is private and can only be accessed by the app itself, and the content written in that mode overwrites the contents of the original file. mode_world_readable indicates that the current file can be read by another application. mode_world_writeable indicates that the current file can be written by another application. Mode_append Checks if a file exists, appends content to the file, or creates a new file. Tip: When using patterns, you can use "+" to select multiple modes, such as Openfileoutput (FILENAME, context.mode_private + mode_world_readable) Android has its own security model, and when the application (. apk) is installed, the system assigns him a userid, which requires a UserID match when the app is going to access other resources such as files. By default, any file created by the app, Sharedpreferences, the database should be privateRead file:gets the input stream through Context.openfileinput. /*
* Name to open or create a file
*/

FileInputStream openfileinput (String name)
To delete a file:    /*
* Name Delete file names
* Return successfully returns True, and vice versa false
*/

boolean DeleteFile (String name)

ways to get the file path:Getfilesdir () can get to "/data/data/<package name>/files" getcachedir () can get to "/data/data/<package name>/cache"
Save the file in SDcard1. Permissions required for Operation SDcard<!--Create and delete files in SDcard permissions --
<uses-permission android:name="Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

<!--write data to SDcard permissions --
<uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE"/>
  2. To store files to SDcard, the program must first determine whether the phone is equipped with sdcard, and can read and write. SDcard status can be obtained by getexternalstoragestate () The returned status is as follows:Environment.media_bad_removal:bad_removal--media Remote before unmount
Environment.media_checking:checking--media present (inserted) and being disk-check
Environment.media_mounted:mounted--media present and mounted and can be read/Write
Environment.media_mounted_read_only:mounted_ro--media present and mounted and can be READ
Environment.media_nofs:nofs--media present but Filesytem don't support
Environment.media_removed:removed--media not present
Environment.media_shared:shared--media present and not mount. and share
Environment.media_unmountable:unmountable--media present but can not mount
Environment.media_unmounted:unmounted--media present
but not mount
3. Get the SDcard directory /*
* Return SDcard Directory file
*/

File getExternalStorageDirectory ()

Android Data Storage--file storage

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.