Android File Operation Method

Source: Internet
Author: User

Common data storage and access methods:
File
SharedPreferences (preference parameter settings)
SQLite Database
Content provider)
Network

Activity (Context)
The Context. getCacheDir () method is used to obtain/data/<package name>/cache directory.
The Context. getFilesDir () method is used to obtain the/data/<package name>/files directory.

Activity (Context) provides the openFileOutput (filename, mode) method for outputting data to a file;
The first parameter is used to specify the file name and cannot contain the path separator '/'
The second parameter is the operation mode:
Context. MODE_PRIVATE: the files created in private operation mode can only be accessed by this program. If the file does not exist, it is automatically created. In addition, the content written to the file overwrites the content of the original file;
Context. MODE_APPEND: the mode checks whether the file exists. If yes, content is appended; otherwise, a new file is created;
Context. MODE_READABLE: indicates that the current file can be read by other applications;
Context. MODE_WRITEABLE: indicates that the current file can be written by other applications;
If you want the file to be read and written by other applications: Pass in (Context. MODE_READABLE + Context. MODE_WRITEABLE)
FileOutputStream outStream = context. openFileOutput (filename, Context. MODE_PRIVATE );
// Param file name, Operation Method

Androidhas a set of security models. When the application program (.apk) is installed, the system will assign a userid. When the application accesses other resources, such as files, it will match the userid. By default, any files created by the application will be created, sharedpreferences, the database is private (the created files are stored in the/data/<package name>/files directory ), only when the specified operation mode is external readable or write can it be accessed by other programs;

Read files:
<1>
FileInputStream inStream = context. openFileInput (filename );
Log. I (TAG, inStream ....)

<2>
Path = "/data/<package name>/files/hello.txt ";
File file = new File (path );
FileInputStream inStream = new FileInputStream (file );
Log. I (TAG, inStream ....)

Ctrl + shift + x/y case

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.