Android File Access

Source: Internet
Author: User

Today, I learned about the file access technology of Android, and many times I need to use file access functions in Android, that is, we can store the desired file, read the file content, and perform other operations on our mobile phone or SD card to achieve the corresponding purpose.

File storage mode:

1. context. mode_private: the default operation mode. This mode indicates that the file is private data and can only be accessed by the application itself. In this mode, the written content will overwrite the content of the original file, if you want to append the written content to the original file, you can use context. mode_append.

2. Context. mode_append: If the mode is changed, the system checks whether the file exists and appends the content to the file. Otherwise, the system creates a new file.

Context. mode_wordl_reable and context. mode_world_writeable are used to control whether other applications have the permission to read and write the file.

3. Context. mode_world_reable: indicates that the current file can be read by other applications.

4. Context. mode_world_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, you can use:

Openfileoutput ("FILENAME", context. mode_world_readble + context. mode_world_writeable );

 

Androidhas a set of security models. When the application program (.apk) is installed, the system will assign a userid to it. When the application wants to access other resource files, it needs to match the userid. By default, files created by any application, sharedpreferences, and databases, should be private and cannot be accessed by other applications. Unless context. mode_world_readable or context. mode_world_writeable is specified during creation, only other programs can access it correctly.

 

Next I will explain the file storage in Android based on my understanding:

1. To create a layout file, we need to implement the following interface. The specific function is to enter the file name and file content and click the Save button to save the file to the mobile phone:

    

2. write code to implement the response function:

The following only posts the core code

1 import Java. io. bytearrayoutputstream; 2 Import Java. io. file; 3 Import Java. io. fileinputstream; 4 Import Java. io. filenotfoundexception; 5 import Java. io. fileoutputstream; 6 Import Java. io. ioexception; 7 8 Import android. content. context; 9 10/** 11 * @ author fanchangfa12 * file operation class: 13 * file storage 14 * file read 15 */16 public class fileoperate {17 context; 18 19 public fileoperate (context) {20 this. Context = context; 21} 22 23 24 25/** 26 * file storage method 27 * @ Param filename file name 28 * @ Param Content File Content 29 * @ throws ioexception 30 */31 public void save (string filename, string content) throws ioexception32 {33 fileoutputstream file_stream = context. openfileoutput (filename, context. mode_private); 34 file_stream.write (content. getbytes (); 35 file_stream.close (); 36} 37 38 public string read (string filename) Throws ioexception39 {40 // get file object 41 file = new file (filename); 42 // get output stream object 43 fileinputstream instream = new fileinputstream (File ); 44 // get the collection of output streams 45 bytearrayoutputstream bstream = new bytearrayoutputstream (); 46 // define the byte array 47 byte [] buffer = new byte [1024]; 48 49 // The length of each data read is 50 int Len = 0; 51 52 while (LEN = instream. read (buffer ))! =-1) 53 {54 bstream. write (buffer, 0, Len); 55} 56 57 instream. close (); 58 bstream. close (); 59 60 61 byte [] DATA = bstream. tobytearray (); 62 Return new string (data); 63 64 // return new string (bstream. tostring (); 65} 66}

3. After the file is saved successfully, the file is saved in/data/package name/files/stored file name by default. For example, I save a.txt file:

    

The core code for reading the response file is also written, and can be implemented by yourself if necessary. In fact, file access in Android is very simple, just like Java data stream operations. Note that different file storage modes apply to different applications with the same permissions.

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.