In-depth understanding of file operation modes in Android Development

Source: Internet
Author: User

I. Basic Concepts Copy codeThe Code is as follows: // context object
Private Context context;
Public FileService (Context context)
{
Super ();
This. context = context;
}
// Method for saving the file
Public void save (String filename, String fileContent) throws Exception
{
FileOutputStream fos = context. openFileOutput (filename, context. MODE_PRIVATE );
Fos. write (fileContent. getBytes ("UTF-8 "));
Fos. close ();
}

Private Mode
① It can only be accessed by the current application that created this file
② If the file does not exist, the file will be created. If the file already exists, the original file will be overwritten.
Context. MODE_PRIVATE = 0;
Append Mode
① Private
② If the file does not exist, the file will be created; if the file exists, the content will be appended at the end of the file.
Context. MODE_APPEND = 32768;
Readable Mode
① The created file can be read by other applications.
Context. MODE_WORLD_READABLE = 1;
Writable Mode
① Allow other applications to write data to it.
Context. MODE_WORLD_WRITEABLE = 2
Ii. Combined Use Copy codeThe Code is as follows: FileOutputStream outStream = this. openFileOutput ("xy.txt", Context. MODE_WORLD_READABLE + Context. MODE_WORLD_WRITEABLE );

Allow other applications to read and write, and overwrite by defaultCopy codeThe Code is as follows: FileOutputStream outStream = this. openFileOutput ("xy.txt", Context. MODE_APPEND + Context. MODE_WORLD_READABLE + Context. MODE_WORLD_WRITEABLE );

Append mode, but allow other applications to read and write

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.