Android development: how to read and write Android files

Source: Internet
Author: User

BKJIA Editor's note: Android consists of four main components: Activity, ContentProvider, Service, and Intent. To run Android files, you need to read and write files from four components. This article describes how to read and write Android files. It is helpful for users who are developing Android.

BKJIA recommended reading: Android development and application details

File storage location

In Android, the file I/O is stored in the/data/<package name>/file/filename directory.

Tip: Android is based on linux. In a linux file system, there is no disk partition similar to Windows. It starts with a forward slash.

Get input and output streams in Android

In Android, stream operations are very simple. There are two methods in the Context class to directly obtain the file input and output stream:

 
 
  1. public FileInputStream   openFileInput  (String name)  
  2. public FileOutputStream   openFileOutput  (String name, int mode)  

As the name suggests, you can get the input and output stream of the file through the above method. There are four modes for the mode in the second method:

◆ Use 0 or MODE_PRIVATE (the default operation): Use 0 to indicate the default value. Only the application that creates the file can access the file. Each file write overwrites the file.

◆ MODE_APPEND to append to an existing file: The append method is used for each file write, similar to the append method in StringBuffer.

◆ MODE_WORLD_READABLE: only read permission.

◆ MODE_WORLD_WRITEABLE: only write permission.

Tip: If you want to get the read and write permissions at the same time, you can create them in mode as follows:

 
 
  1. MODE_WORLD_READABLE+ MODE_WORLD_WRITEABLE 
Supplement Java SE

FileOutputStream:

Public void write (byte [] B) throws IOException this method can write the specified byte array to the file output stream

FileInputStream:

Public int read (byte [] B) throws IOException this input stream reads data of up to B. length bytes into a byte array. This method is blocked before some input is available.

You can directly use the write method for the output stream. refer to the following code:

Java code

 
 
  1. <Span style = "white-space: pre;"> </span> /**
  2. * Write data
  3. * @ Param fs
  4. * @ Param content
  5. */
  6. Public void fileWrite (FileOutputStream fos, String content ){
  7. Byte [] contentcontentByteArray = content. getBytes ();
  8. Try {
  9. Fos. write (contentByteArray );
  10. } Catch (IOException e1 ){
  11. E1.printStackTrace ();
  12. }
  13. Try {// close the stream
  14. Fos. close ();
  15. } Catch (IOException e ){
  16. E. printStackTrace ();
  17. }
  18. }
  19.  

For the performance of the input stream, you can use ByteArrayOutputStream to create a character array in the memory. After reading the file, read the data. See the following code:

Java code

 
 
  1. * Read data
  2. * @ Param FCM
  3. * @ Return
  4. */
  5. Public String fileRead (FileInputStream ISI ){
  6. ByteArrayOutputStream baos = new ByteArrayOutputStream ();
  7. Byte [] buffer = new byte [1024];
  8. Int len =-1;
  9. Try {
  10. While (len = (FS. read (buffer )))! =-1 ){
  11. Baos. write (buffer, 0, len );
  12. }
  13. } Catch (IOException e ){
  14. E. printStackTrace ();
  15. }
  16. String result = new String (baos. toByteArray ());
  17. // System. out. println (result );
  18. Try {
  19. Baos. close ();
  20. FCM. close ();
  21. } Catch (IOException e ){
  22. E. printStackTrace ();
  23. }
  24. Return result;
  25. }

ByteArrayOutputStream: This class implements an output stream, where data is written into a byte array.

Public void write (byte [] B, int off, int len) writes the len bytes starting from offset off in the specified byte array to the output stream of this byte array.

Refer to the Code for downloading FileIO code.

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.