Android reads and writes files

Source: Internet
Author: User

Android in all sets has its own security model. For details, see Android development documentation.
.. When the application program (.apk) is installed, a userid is assigned. When the application wants to access other resources, such as files, userid matching is required. By default, files, databases, and sharedpreferences created by any application should be private (in/data/your_project/files/) and cannot be accessed by other programs. Unless mode_world_readable or mode_world_writeable is specified during creation, the other programs can be accessed correctly only in this way.

Because of this security guarantee, Android requires that the user ID of the process be checked when the process opens the file. Therefore, Java APIs cannot be used directly to open it, Because Java Io functions do not mention this mechanism.

// You cannot open the program directly using Java APIs.Private Data

The default path is/data/your_project/files/
Filereader file = new filereader ("android.txt ");

Private Data is particularly emphasized here! The implication is that if a file or data is not private to the program, and you do not need to go through the android permission check when accessing it, you can still directly access it using Java I/O APIs. The so-called non-private data is only files or data stored on sdcard,

// You can use Java I/O APIs to directly open files on sdcard.

Filereader file = new filereader ("/sdcard/android.txt ");

 

 

To open your own private files and data, you must use the openfileoutput and openfileinput methods provided by activity.

// Create a private file of the program. Due to permission requirements, you must use the method provided by activity.

Fileoutputstream OS = This. openfileoutput ("android.txt", mode_private );
Outputstreamwriter outwriter = new outputstreamwriter (OS );

 

// Read private files of the program. The method provided by activity must be used due to permission requirements.

Fileinputstream OS = This. openfileinput ("android.txt ");
Inputstreamreader inreader = new inputstreamreader (OS );

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.